views:

1997

answers:

4

Is there a recommended way to bounce an asp.net application besides touching web.config from inside the application? is HttpRuntime.UnloadAppDomain(); the preferred way to do this ? and if so where do you do this? in the unload of a page or some other place in the application

+5  A: 

You can stop and start the Application Pool associated with the app as well.

John Sheehan
but this is from IIS right. in some cases I might not have access to the IIS box so I am looking for something programatic.
MikeJ
You can recycle the application pool using WMI: http://blogs.iis.net/chrisad/archive/2006/08/30/Recycling-Application-Pools-using-WMI-in-IIS-6.0.aspx
Portman
cool. i did not know that
MikeJ
+5  A: 

If this is .NET 2.0 or greater, then you can add in an "App_offline.htm" file, make a request to the server, remove it, and then make another request to the server.

This sequence of events will force ASP.NET to unload the application for as long as the app_offline.htm file exists in the folder.

Scott Guthrie's blog entry on it: http://weblogs.asp.net/scottgu/archive/2005/10/06/426755.aspx

Stephen Wrighton
doesnt htis just take the system offline. I want it to restart/reload with clean cache/session and the like. when I remove the app_offline.htm this will bring it back up by default?
MikeJ
@MikeJ - no. It shuts down the app and unloads the app domain from the server until the file is removed.
Stephen Wrighton
@MikeJ - Also please note that this is NOT a programmatic change. You'll need to physically add and remove the file for it to work
Stephen Wrighton
+5  A: 

Touching web.config from inside an application is a bad idea, IMO. Also, the idea of having a file that you modify is a little hackney, IMO.

The documentation specifically states that UnloadAppDomain will shut the application down:

UnloadAppDomain allows programmatic shutdown of unused applications.

You should be able to make this call anywhere in the application. Mind you, you might get a SecurityException, so make sure that the runtime gives you the appropriate permissions (you might want to put this in a library and make a call and then set the library up in the GAC with evidence to give it full trust).

casperOne
yes. this is why I was asking. web.config modification seemed ugly and likely to get us written up on thedailyWTF.com :)
MikeJ
I think a better line to quote would be: "Terminates the current application. The application restarts the next time a request is received for it." The "unused" part in that quote could be read as "will only shut down if no one is using it", which is not the case.
Zhaph - Ben Duguid
Use System.Web.HttpRuntime.UnloadAppDomain()
HasanGursoy
A: 

If you don't want to stop and start the app pool you can always recycle it.

Andrew Hare