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?
7 Answers
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).

- 73,706
- 19
- 184
- 253
-
3yes. this is why I was asking. web.config modification seemed ugly and likely to get us written up on thedailyWTF.com :) – MikeJ Jan 02 '09 at 19:46
-
2I 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 Jan 02 '09 at 20:33
-
2Use System.Web.HttpRuntime.UnloadAppDomain() – HasanG Oct 10 '10 at 11:43
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

- 36,783
- 6
- 67
- 86
-
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 Jan 02 '09 at 19:47
-
@MikeJ - no. It shuts down the app and unloads the app domain from the server until the file is removed. – Stephen Wrighton Jan 02 '09 at 21:30
-
3@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 Jan 02 '09 at 21:31
this code work for me. just call it to reload application.
System.Web.HttpRuntime.UnloadAppDomain();
This method will just unload our application. If you just put this method in an ASP.NET web button you are totally done. So when will our application reloaded? Actually if you click your button it will first launch our method and unload application. Further on the web page we are on at that moment will be reloaded as well, because we just clicked a button and the web page should refresh. After launching our method the page refresh process will cause our application to reload as well.

- 2,611
- 1
- 26
- 34

- 3,365
- 4
- 36
- 57
-
1Seems to work well, and definitely the simplest solution given here! – David Kirkland Feb 12 '16 at 12:50
You can stop and start the Application Pool associated with the app as well.

- 77,456
- 30
- 160
- 194
-
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 Jan 02 '09 at 19:48
-
1You 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 Jan 03 '09 at 03:14
-
3
You can do this by calling the HttpRuntime.ShutdownAppDomain method (you will need to use reflection to invoke it, since it is a private static method)
See How to restart an IIS Worker Process programmatically (i.e. shutdown the current ASP.NET Domain) for an example of how I use this method in a 'Restart' REST API

- 4,161
- 2
- 31
- 49
You could safely restart a web application by creating or renaming a folder at run time under the application directory. Obviously you need to give the user assigned to run the application "modify" rights to the web directory or to a sub directory under it.
the method is mentioned at http://www.bartlannoeye.be/blog/restarting-a-.net-web-application-without-restarting-iis
I used the following code to do it in my case. Modify it to work on a "writable" sub-directory
protected void RestartButton_Click(object sender, EventArgs e)
{
//restart web app (instead of iisreset)
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("restart"));
if (dir.Exists)
{
Directory.Move(dir.FullName, dir.FullName + "ed");
}
else
{
DirectoryInfo dired = new DirectoryInfo(Server.MapPath("restarted"));
if (dired.Exists)
{
Directory.Move(dired.FullName, dir.FullName);
}
else
{
Directory.CreateDirectory(dir.FullName);
}
}
}

- 91
- 1
- 2
-
well this doesnt work for me. I see the directory being created but the application_Start event is not called upon! – KMX Sep 19 '13 at 00:52
-
If you don't want to stop and start the app pool you can always recycle it.

- 344,730
- 71
- 640
- 635