1

We have an ASP.NET (3.5 SP1) application running on IIS7 / Windows 2008. We trap Application_Start and Application_End events in Global.asax. We also host WCF services in the app and trap the OnOpening and OnClosing events via a ServiceHostFactory. Thus, we thought, we are guaranteed notification of any application start and stop, scheduled or unscheduled.

Several days ago our application caught an Application_Start event while in a 'started' / 'running' state.

No Application_End event preceeded the Application_Start (or even followed it a few minutes later, if race conditions are considered).

Our first thought was that our application actually silently crashed and terminated. Actually what happened was a new App Domain spun up to service inbound requests but the existing App Domain's background threads (we do lot's of stuff in ThreadPool threads) remained running for several days -- until I killed them with an IISRESET.

The guess is Application_End did not fire because the original AppDomain did not end... but then why did Application_Start fire?

Looking for a tip or document describing how this semi-shutdown+AppDomainStartup mechanism works in ASP.NET.

Thanks in advance,

Howard Hoffman

Howard Hoffman
  • 897
  • 1
  • 9
  • 22

1 Answers1

0

Please learn about application pool recycle, which can lead to such situations. When IIS determines to recycle a pool, it simply initializes a new worker process first (Application_Start will be called in turn), and then shuts down the old (Application_End).

I suggest you add some application level logging with process id in logs, to better understand if my above analysis is right.

For ASP.NET developers, learning more about IIS is recommended.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • Curiously, I'm seeing the same behavior -- multiple Application_start invocations, and we are logging pid, and it is the same. So the same w3wp process decides to start an app that it is already serving again. – MK. Jan 27 '11 at 21:57
  • http://blogs.msdn.com/b/tess/archive/2008/11/06/troubleshooting-appdomain-restarts-and-other-issues-with-etw-tracing.aspx – Lex Li Apr 15 '11 at 02:50
  • I did want to add that our AppPool was set to never recycle - all of the AppPool recycle settings were at 0. We never did provably determine what actually happened - why the crash of the App Pool. We did later alter our Application_Start/End mechanism to use a File as a "Mutex" -- App-Start creates the file and App-End deletes, and App-Start quickly terminates if the file exists. So we could at least detect the double-start case should it occur again. In a couple of years of production since...it has not. – Howard Hoffman Jan 30 '13 at 03:13