[ACCEPTED]-how to restart asp.net application besides modifying web.config-restart
Touching web.config from inside an application 12 is a bad idea, IMO. Also, the idea of having 11 a file that you modify is a little hackney, IMO.
The 10 documentation specifically states that UnloadAppDomain
will 9 shut the application down:
UnloadAppDomain 8 allows programmatic shutdown of unused applications.
You 7 should be able to make this call anywhere 6 in the application. Mind you, you might 5 get a SecurityException
, so make sure that the runtime gives 4 you the appropriate permissions (you might 3 want to put this in a library and make a 2 call and then set the library up in the 1 GAC with evidence to give it full trust).
If this is .NET 2.0 or greater, then you 7 can add in an "App_offline.htm" file, make 6 a request to the server, remove it, and 5 then make another request to the server.
This 4 sequence of events will force ASP.NET to 3 unload the application for as long as the 2 app_offline.htm file exists in the folder.
Scott 1 Guthrie's blog entry on it: http://weblogs.asp.net/scottgu/archive/2005/10/06/426755.aspx
this code work for me. just call it to reload 12 application.
System.Web.HttpRuntime.UnloadAppDomain();
This method will just unload 11 our application. If you just put this method 10 in an ASP.NET web button you are totally 9 done. So when will our application reloaded? Actually 8 if you click your button it will first launch 7 our method and unload application. Further 6 on the web page we are on at that moment 5 will be reloaded as well, because we just 4 clicked a button and the web page should 3 refresh. After launching our method the 2 page refresh process will cause our application 1 to reload as well.
You can stop and start the Application Pool 1 associated with the app as well.
You can do this by calling the HttpRuntime.ShutdownAppDomain method (you 4 will need to use reflection to invoke it, since 3 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 2 example of how I use this method in a 'Restart' REST 1 API
You could safely restart a web application 8 by creating or renaming a folder at run 7 time under the application directory. Obviously 6 you need to give the user assigned to run 5 the application "modify" rights 4 to the web directory or to a sub directory 3 under it.
the method is mentioned at http://www.bartlannoeye.be/blog/restarting-a-.net-web-application-without-restarting-iis
I used 2 the following code to do it in my case. Modify 1 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);
}
}
}
If you don't want to stop and start the 1 app pool you can always recycle it.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.