Can anybody tell me when Application_End is triggered in a lifecycle of an application? When all sessions are ended, will Application_End be triggered automatically? + Are there any other reasons why Application_End could be triggered?

- 59,778
- 26
- 187
- 249

- 25,140
- 52
- 153
- 244
-
2I deleted my answer that Application_End only fires when you shut down the website. A few minutes of research, however, yielded conflicting information on this issue and I don't wanna get down-voted for having a wrong answer. – Cybis Nov 13 '08 at 21:38
-
1Not an answer to your question, but a useful too to investigate if this event is happening and you're not sure why: http://blogs.msdn.com/b/tess/archive/2006/08/02/asp-net-case-study-lost-session-variables-and-appdomain-recycles.aspx – Tao Mar 28 '14 at 09:46
3 Answers
The application_end
event primarily fires when the IIS pool is recycled or the application itself is unloaded. One other thing to note, that a change to a dependent file (say web.config) will cause the application to reload itself, which will in cause the application_end event to fire while it is closing itself off.
To note, the only instance I found of the application end event firing when the last user session times out is in some old documentation dated 2001. I'm not sure if that criteria still applies.

- 29,277
- 14
- 101
- 140
-
2I noticed when the web.config is modified, it may takes a few minutes before the application_end is fired. – an phu Dec 01 '11 at 20:30
-
To @anphu's point, when the application reloads due to a config change (or DLL change), there is often a period of time where both the old instance of the application and the new instance of the application are running side-by-side, as the old instance finishes handling its existing requests; this is nicely explained at http://odetocode.com/articles/305.aspx - good reading for this topic – Tao Mar 28 '14 at 09:15
-
Will application end be called if I uninstall web application from command prompt that was installed using msi package? – OldSchool Jan 24 '18 at 17:03
-
@YakRangi - I believe so, since the process of uninstalling would involve shutting down and removing the web app instance from IIS – Dillie-O Jan 26 '18 at 14:02
-
@Dillie-O I was testing this, but have a weird observation. In my Application_End I need to call a WCF service. Sometimes its able to call it other time it throws type initializer exception. I have my WCF reference in another project than the web application itself (where call to WCF service is made). I have WCF service endpoints configured in my Web.config. But I am not sure why sometimes its able to call and sometimes not. I am suspecting unloading of web service project (from client side) and call Application_End are in race? Any ideas? – OldSchool Jan 28 '18 at 13:11
-
@YakRangi - Where does the WCF service reside and why are you needing to call it at Application_End. Generally speaking this event shouldn't be used for complex operations, since the web app is in a "tear down" state. Typically you use it to release local resources that may be in use. – Dillie-O Jan 29 '18 at 00:30
-
@Dillie-O Yes, you are right. But our requirement is bit different. At Application_End we need to signal the service that client is going down gracefully. Service is residing on the same machine. – OldSchool Jan 29 '18 at 05:25
Application_End is triggered when the ASP.NET worker process terminates. This usually occurs after a configurable period of inactivity or when IIS (or the relevant application pool) is shut down or restarted.
When running in IIS 6.0, the inactivity timeout is configurable through the application pool settings in Internet Services Manager (Idle Timeout on the Performance tab). Under earlier versions of IIS it can be set in machine.config (idleTimeout under processModel).

- 5,079
- 1
- 17
- 6
-
2Do you know if there is an instance where the process could terminate without running Application_End. For example, I have mapped a network share in Application_Start, and I want to disconnect that share on Application_End. Will there every be a point at which the application could end without triggering Application_End? I imagine a power outage would satisfy this criteria. What about a fatal application error of some kind? – crush Oct 28 '13 at 15:57
-
3@crush: yes, absolutely, whenever the process terminates "unexpectedly" the event will not fire; examples include power outage/hardware failure, any situation where a thread in the process "hangs" and the app pool terminates (eg IISReset if the longest request takes more than 30 secs to complete), any case where someone or something manually does "End Process" on that w3wp.exe process (eg in Task Manager), any "Force Close", and any situation where unmanaged code causes memory or other process-level errors (stack overflow, GPF, etc) – Tao Mar 28 '14 at 08:50
In my case, the Aapplication Pool was being recycled because the property Idle Time-Out (minutes)
The Default value of this property is 20 (minutes)

- 101
- 1