15

... will the Application_Start method be ran again for the next request(s) or not?

Does it depend on ASP.NET version, hosting server version and/or other context?

I am trying to determine if it's a good thing to load critical assemblies there or not. For example data access assemblies which are vital to the functioning of the whole application or not. Failure to load such assembly would make subsequent requests useless.

Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
  • You handle the error - I load all the critical assemblies on start, but also check them again in other critical points that are loaded, in case that fail to load on the start – Aristos May 03 '11 at 23:05

1 Answers1

10

Application_Start will be fired only once for each web application so in your case, the start will not happen again for subsequent requests.

Typically, I prefer to put one time start-up code in the application start within try-catch and if there is an exception then set the global error flag. In each BeginRequest, the flag is checked and if it is set, user is redirected to a custom error page indicating the site is down and please contact the administrator.

VinayC
  • 47,395
  • 5
  • 59
  • 72
  • 1
    @Alex, it's simple private static boolean variable in global.asax that gets set in `application_start` in case of error. In `BeginRequest`, the flag is checked to see if there was an error during application start-up and if yes then redirect to error page. – VinayC Jul 25 '11 at 05:03
  • Ah, now I get it. Thought you use a build in flag of ASP.NET. Thanks! – Alex Jul 25 '11 at 19:48
  • 1
    If i throw an exception in Application_Start, the next request hits Application_Start again. If I catch the exception, and do not rethrow then the next request does NOT hit Application_Start .... this is 4.5.2 CLR on IIS8 Express – felickz Sep 22 '14 at 13:36
  • I think that unhandled exception is taking the app domain down (you should able to see `Application_End` event), hence you will see app start event on subsequent requests. – VinayC Sep 24 '14 at 11:37
  • 4
    @felickz I can confirm both cases - yours and Vinay's on two different hosts. On an IIS6+CLR4, App_Start is called only once. On IIS8+CLR4.5, App_Start is called every time if there was an exception. So I'm guessing the behavior is changed either with IIS, or with CLR versions. – Ishmaeel Jun 26 '15 at 08:24
  • Does an error in Application_Start actually prevent subsequent requests from being served? None of this answers this most basic question of what the result of an unhandled error is (i.e. no requests served thereafter). It sounds like you're handling the error, and setting a flag yourself. This doesn't answer what happens when an unhandled error occurs. – Triynko Aug 07 '18 at 13:16
  • @Triynko, Application_Start (and _End) is linked with the appdomain serving the application. So to answer your question, AFAIK, error in Application_Start does not prevent subsequent requests. However, unhandled exception can take down the appdomain - this depends upon the type of exception and exact behavior regarding the same has changed across .NET versions. So if the exception in application_start shut downs the appdomain then next request will again trigger application_start and shutdowns appdomain again i.e. no request will be served. – VinayC Aug 08 '18 at 13:00