how to restart asp.net application besides modifying web.config

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

this code work for me. just call it to reload application.

System.Web.HttpRuntime.UnloadAppDomain();

Read more

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.

Method 4

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

Method 5

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

Method 6

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);
        }
    }
}

Method 7

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x