Restart ASP.NET Application Programatically

You can use the  HttpRuntime.UnloadAppDomain() which will terminate the application and of course remove all the session objects for all users the next time a request is being received.

Demonstration:
Create a dummy web application, drag and drop two buttons. and use some like below

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
       Session.Add("ASP","hello");
}
private void Button1_Click(object sender, System.EventArgs e)
{
        Response.Write(Session["ASP"].ToString());
        HttpRuntime.UnloadAppDomain();
}
private void Button2_Click(object sender, System.EventArgs e)
{
        Response.Write(Session["ASP"].ToString());
}


When you click Button2, you will have object reference not set to an instance of an object error which indicates that Session["ASP"] is equal to null.

Comments