MikeJ
MikeJ

Reputation: 14565

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?

Upvotes: 30

Views: 34882

Answers (7)

Fred
Fred

Reputation: 3441

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.

Upvotes: 17

user2431693
user2431693

Reputation: 91

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

Upvotes: 2

Dinis Cruz
Dinis Cruz

Reputation: 4289

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

Upvotes: 5

casperOne
casperOne

Reputation: 74560

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

Upvotes: 26

Andrew Hare
Andrew Hare

Reputation: 351758

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

Upvotes: 0

Stephen Wrighton
Stephen Wrighton

Reputation: 37880

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

Upvotes: 18

John Sheehan
John Sheehan

Reputation: 78152

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

Upvotes: 7

Related Questions