Reasons for ASP.NET application restarts on IIS server

Common reasons for causing ASP.NET web applications to restart

If you were experiencing you ASP.NET Web Application to restart, there are few possible reasons for that, but before we jump to resons why ASP.NET web application hosted on IIS gets restarted let's first look at the relation between IIS and ASP.NET

IIS-isolation

As you can see,

  • One AppPool and be use for hosting multiple Web Applications
  • Aach AppPool can issue one or multiple working processes depending on the AppPool settings (Maximum Workwer Processes [maxProcesses], default is 1)
  • Worker processes can host one or more web applications loaded into separate AppDomain (isolation)

Note

It is not a good practice to use single AppPool for hosting multiple web applications. Despite isolation in AppDomains, web applications are still running under same process which hosts AppDomains.

Based on these dependecies, you can see that there are different levels of application restarts. This also introduces two types of recycling, the one that recycles AppDomain and AppPool recycling.

Difference between AppDomain and AppPool recycling

AppDomain recycle/restart

Depending on how many websites you have using same AppPool, you will have AppDomain loaded to worker process for each application. So restarting one application does not mean the other which use the same worker will be restarted as well.

AppDomain of application that has been recycled will have it's AppDomain unloaded and reloaded to the worker process. This will cause loss of all in-memory data.

Note

If sessionState mode is set to InProc, AppDomain recycle will cause all session states to be lost. Consider updating sessionState mode to Custom using Redis Session State Provider https://docs.microsoft.com/en-us/azure/redis-cache/cache-aspnet-session-state-provider

Now let's see what can cause Application Domain recycling:

1. Updating web.config

Configuration from web.config are loaded to AppDomain level in the application so in order to pick up new values your application will restart and any in memory data along with session states (in case they are inproc) will be lost.

3. Updating Global.asax

The Global.asax file is in the root of application directory. Although it is atomatically added to the project it is not required to have it. It provides usefull application event handlers that can be used to track application behaviour during the runtime.

Unlike AppPool recycle, AppDomain restart does not write any log, so you cannot trace it out of the box. However Gloabal.asax provides application events to which you can add handlers and perform actions such as logging in order to track application stop and start caused by AppDomain recycling

  • Application_Init
    The Application_Init event is fired when an application initializes the first time.
  • Application_Start
    The Application_Start event is fired the first time when an application starts.
  • Session_Start
    The Session_Start event is fired the first time when a user’s session is started. This typically contains for session initialization logic code.
  • Application_BeginRequest
    The Application_BeginRequest event is fired each time a new request comes in.
  • Application_EndRequest
    The Application_EndRequest event is fired when the application terminates.
  • Application_AuthenticateRequest
    The Application_AuthenticateRequest event indicates that a request is ready to be authenticated. If you are using Forms Authentication, this event can be used to check for the user's roles and rights.
  • Application_Error
    The Application_Error event is fired when an unhandled error occurs within the application.
  • Session_End
    The Session_End Event is fired whenever a single user Session ends or times out.
  • Application_End
    The Application_End event is last event of its kind that is fired when the application ends or times out. It typically contains application cleanup logic.

4. Changing the structure of application folder

Except for AppData folder, adding, removing or renaming any of the folders inside your WebApplication folder will cause your application to restart. In case you really need to make new folders and files, stick to AppData folder

2. Changing the content of bin folder

Any change of the bin folder will causee AppDomain to be unloaded and reloaded to worker process. Updating bin folder basically means that you are about to change loaded assemblies of the AppDomain, so IIS will reload your AppDomain on order to pick up those changes.

3. Updating non precompiled files (*.aspx.cs, *.cstml)

If you are running a web application that contains big number of not pre-compiled files such as WebForms code-behind page files *.aspx.cs of MVC Razor views *.cshtml, you might run into a problem of continuous application restarts. The dault value is 15, so if you reach this number application will perform restarts. This value is available on the global and local level (machine.config and web.config). For more info you can check MSDN article https://msdn.microsoft.com/en-us/library/s10awwz0(v=vs.71).aspx

<system.web>
	<compilation targetFramework="4.6.2" numRecompilesBeforeAppRestart="50" />
</system.web>
    

Increasing the number will solve the application restarts, but it will add more memory consumption of your application, so finding the right value for this property depends on the number of non-precompiled files your web application or website has

4. Updating machine.config

Note

machine.config is common for all websites on the host, so changing it will affect all websites on the host

Depending on framework and architecture machine config should be in the following location on your machine

32-bit
%WinDir%\Microsoft.NET\Framework\[.NET Framework Version]\CONFIG\machine.config

64-bit
%WinDir%\Microsoft.NET\Framework64\[.NET Framework Version]\CONFIG\machine.config

If you are not sure abuout the location of machine confign on your host you can just run the followong PowerShell command which will give you output witht he machine.config full path.

[System.Runtime.InteropServices.RuntimeEnvironment]::SystemConfigurationFile
    

Any update of machine.config file will cause all AppPools to recycle in order to load new configuration

AppPool recycle

Any of the above mentioned changes will not trigger AppPool recycling. Application Pool recycling can be triggered manually from Internet Information Services (IIS) Management of from recycling setting of the application pool accesible from the same tool.

Apppool Recycling

References

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

PowerShell

read more

Comments for this article