Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Application_End isn't called in asp.net WebService

I know that in the following cases Apllication_end will be called:

  1. edit the config file for an application that's running.
  2. change a dll in the bin directory.
  3. stop (or restart) IIS.
  4. Process Recycling turned on either in IIS6 App Pools, or using the aspnet worker process.

but my question is what are the cases when it's won't be called?
I know it won't be called if you manually End w3wp process or if the server will brutally shut down.

Is there any other scenarios?

Thanks!

like image 673
Dor Cohen Avatar asked Oct 09 '22 00:10

Dor Cohen


2 Answers

The answer to your question is basically in your question already. Application_End is called anytime the process hosting your web service shuts down gracefully.

The only time this would happen is:

  1. an action caused IIS to restart the service process (changing a file would force this)
  2. the IIS service is shut down
  3. process recycling.

If the process is terminated abruptly (i.e killing the w3wp process), it won't get a chance to run.

like image 101
pdriegen Avatar answered Oct 10 '22 13:10

pdriegen


It seems that the brutal shut down can be caused by OutOfMemoryException that caused failure in creating AppDomain that are needed for executing requests. In this scenario Application_end won't be called.

Event Type: Error
Event Source: ASP.NET 2.0.50727.0
Event Category: None
Event ID: 1334
Date: 4/22/2012
Time: 11:23:13 AM
User: N/A
Computer: CCBSHAIS02
Description:
Failed to initialize the AppDomain:/LM/W3SVC/1/Root/AgentWS

Exception: System.SystemException

Message: Failed to create AppDomain.

StackTrace: at System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironment(String appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters) at System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironmentAndReportErrors(String appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)

InnerException: System.OutOfMemoryException

Message: Exception of type 'System.OutOfMemoryException' was thrown.

StackTrace: at System.AppDomain.nCreateDomain(String friendlyName, AppDomainSetup setup, Evidence providedSecurityInfo, Evidence creatorsSecurityInfo, IntPtr parentSecurityDescriptor) at System.AppDomain.CreateDomain(String friendlyName, Evidence securityInfo, AppDomainSetup info) at System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironment(String appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)

for further reading on IIS and AppDomains look at: http://weblogs.asp.net/owscott/archive/2007/09/02/application-vs-appdomain.aspx

like image 43
Dor Cohen Avatar answered Oct 10 '22 13:10

Dor Cohen