30

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 7

26

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

Sign up to request clarification or add additional context in comments.

3 Comments

yes. this is why I was asking. web.config modification seemed ugly and likely to get us written up on thedailyWTF.com :)
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.
Use System.Web.HttpRuntime.UnloadAppDomain()
18

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

3 Comments

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 - no. It shuts down the app and unloads the app domain from the server until the file is removed.
@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
17

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.

1 Comment

Seems to work well, and definitely the simplest solution given here!
7

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

3 Comments

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.
You can recycle the application pool using WMI: blogs.iis.net/chrisad/archive/2006/08/30/…
This would recycle every app using the same AppPool.
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

1 Comment

Looks like this link is expired or broken.
2

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

2 Comments

well this doesnt work for me. I see the directory being created but the application_Start event is not called upon!
That Bart Lannoeye link seems to be dead
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.