views:

34

answers:

2

In our Application_Start event handler we're performing some actions that intermittently fail due to file locking issues. In this scenario we would like to return the application to an "un-started" state.

By this I mean that the user will be shown an error page, and then the next time a user hits the site the Application_Start event will be fired again.

We're using ASP.NET 3.5, WebForms and MVC.

A: 

I'm pretty sure that there is no way to quit/force stop an webForms application from within the application. However, an ungentle way to prevent the application from starting is to generate an unhandled error within your application_start. This will prevent start up from happening and upon the next hit to your webForms app, the application_start will try again.

Tommy
The thing is that the ASP .NET won't fire a Application_Start event if the event threw an exception on the first run.
klashar
+3  A: 

AppDomain.Unload offers what you're looking for, but I wouldn't recommend it.

There are lots of catches; tearing down an app domain programmatically is not without its own set of issues (i.e. if a thread's blocked in native code you may see a CannotUnloadAppDomainException) and is generally a poor design, IMO.

What you're attempting to do is highly unconventional; I would reconsider the approach all together. If you just need to execute some code once at the app domain level, there are lots of better ways to do it, like statics for instance or a flag in the HttpRuntime cache. Just mind the web-garden and concurrency scenarios.

Good luck.

Nariman