0

I'm using Windows 7, IIS 7.5.7600.16385 and currently .NET 4.6.1 is installed and we have a MVC application.

Some days ago we had some strange behavior at our application. Unfortunately a service which is called inside Application_Start was not available and an unhandled exception was thrown inside. My expected behavior was that the Application_Start() is called again with the next request or the the next request is starting directly with Application_BeginRequest() like mentioned in What happens if an unhandled exception is thrown in Application_Start?.

Unfortunately I get the following result:

In case of exception inside Application_Start() I get an error 500 at the first request. That's ok.

After this all other requests are returning the exception which is thrown at the first request. I verified it by throwing an exception with timestamp inside at my local environment. Each response contains the exception with the timestamp from first request and the HTTP answer is still 500. It has no dependency which url is called. At our code no breakpoint is hit but the IIS log show the requests. It seems that the answer is cached somewhere.

Personally I like the behavior because the application doesn't respond to the requests with undefined initialization status.

And yes I know that calling other service resources inside Application_Start() is not the best idea and we will probably remove it next time :)

My Questions:

  • Is it possible to configure the behavior in case of an exception is thrown at Application_Start()?

  • Maybe somebody know when this behavior was changed or does it exists already a long time?

Community
  • 1
  • 1
RRossberg
  • 1
  • 1

2 Answers2

0

Well I analyzed this scenario and search through many sites, but coould not find any info about it. However, I managed to observe so behavior:

  • When unhandled error is thrown inside Application_Start then IIS returns error page and web app starts to shut down.
  • During the shutdown (in my case that was 10 sec.) any new request are handled by IIS and the response is the same as in the first request. If you think about it its logical, because IIS knows that website is shutting down so its obvious that last error cause it.
  • After some time application raises Application_End event to let know that shutting down is complete. After that event the next request to the website will raise Application_Start again and new response will be generated.

I don't think you can alter this behavior, because application just need some time to restart.

Lesmian
  • 3,932
  • 1
  • 17
  • 28
  • Hi Lesmian. Thank you for your answer. I installed on my private computer IIS Express 8 to test it again with a clean installation and you're right. After exception in Application_Start it takes about 10 seconds and in this time the exception is part of the response. – RRossberg Mar 17 '16 at 19:40
  • Unfortunately this is not the same behavior I got at my business computer and server. At the office I used the same test app but it was not calling Application_End within 10 minutes. And the reported application from our live server didn't called it within 4 hours (tested on local machine and the same exception was shown also after 30 minutes). But your explanation sounds good and I will check why Application_End was not called at the business installation (checking IIS 7 vs. IIS 8, installed modules and so on). – RRossberg Mar 17 '16 at 20:00
0

Today I had some time to check the behavior again. We introduced Serilog some releases ago and it seems that the configuration has an effect to the restarting behavior.

protected void Application_Start()  
{  
  SerilogManager.Configure(); //own class  
  using (LogContext.PushProperty(SerilogManager.PROPERTY_NAME_ComponentName, "xxx")){}    
  throw new Exception(DateTime.Now.ToString("hh:mm:ss"));  
}  

If I remove the PushProperty line from Application_Start then the restart will work without any problem. With this line no Application_End will be called.

Now I can reproduce it on private und business computer. Not sure why my demo application didn't call Application_end on my business machine last time.

RRossberg
  • 1
  • 1