Programmatically shutdown and ASP.NET web app.

A

Andrew

I have ASP.NET 2.0 application. I'm running some initialization code in
Application_Start() in Global.asax. If this code fails I would like to
shutdown the application completely. Meaning no page, or an error page is
returned to the user. I've tried HostingEnvironment.InitiateShutdown() but
it's async and still returns a page to the user. According to msdn this
method is equivalent to ApplicationManager.ShutdownApplication()
(http://msdn2.microsoft.com/en-US/li...cationmanager.shutdownapplication(VS.80).aspx).
My results are consistent with this.

Any ideas on how to programmatically shutdown an ASP.NET app are
appreciated.

Thanks,
Andrew
 
K

Ken Cox - Microsoft MVP

Write the following line into your web.config file in system.web:

<!--- system.web goes here -->
<httpRuntime enable="false" />

The code is like this:

Configuration config = OpenWebConfiguration(ApplicationPath);
HttpRuntimeSection httpRuntimeSection = (HttpRuntimeSection)
config.GetSection("system.web/httpRuntime");
bool httpRuntimeSectionEnabled = httpRuntimeSection.Enable;

httpRuntimeSection.Enable = !httpRuntimeSectionEnabled;
SaveConfig(config);

public void SaveConfig(Configuration config) {
RemotingManager.ShutdownTargetApplication();
config.NamespaceDeclared = true;

// check if session expired
if (String.IsNullOrEmpty(ApplicationPath) ||
String.IsNullOrEmpty((string)Session[APP_PHYSICAL_PATH])) {
Server.Transfer("~/home2.aspx");
}

config.Save(ConfigurationSaveMode.Minimal);
}
 
A

Andrew

Hi Ken,

Thanks for such a quick response. In the code you posted it appears as
though settings are being saved to web.config so IIS will recycle the app.
Is this correct? Is there more going on?



Ken Cox - Microsoft MVP said:
Write the following line into your web.config file in system.web:

<!--- system.web goes here -->
<httpRuntime enable="false" />

The code is like this:

Configuration config = OpenWebConfiguration(ApplicationPath);
HttpRuntimeSection httpRuntimeSection = (HttpRuntimeSection)
config.GetSection("system.web/httpRuntime");
bool httpRuntimeSectionEnabled = httpRuntimeSection.Enable;

httpRuntimeSection.Enable = !httpRuntimeSectionEnabled;
SaveConfig(config);

public void SaveConfig(Configuration config) {
RemotingManager.ShutdownTargetApplication();
config.NamespaceDeclared = true;

// check if session expired
if (String.IsNullOrEmpty(ApplicationPath) ||
String.IsNullOrEmpty((string)Session[APP_PHYSICAL_PATH])) {
Server.Transfer("~/home2.aspx");
}

config.Save(ConfigurationSaveMode.Minimal);
}


Andrew said:
I have ASP.NET 2.0 application. I'm running some initialization code in
Application_Start() in Global.asax. If this code fails I would like to
shutdown the application completely. Meaning no page, or an error page is
returned to the user. I've tried HostingEnvironment.InitiateShutdown()
but it's async and still returns a page to the user. According to msdn
this method is equivalent to ApplicationManager.ShutdownApplication()
(http://msdn2.microsoft.com/en-US/li...cationmanager.shutdownapplication(VS.80).aspx).
My results are consistent with this.

Any ideas on how to programmatically shutdown an ASP.NET app are
appreciated.

Thanks,
Andrew
 
A

Andrew

Hi Ken,

It's a bit abrasive but "AppDomain.Unload( AppDomain.CurrentDomain );" seems
to do the trick.



Ken Cox - Microsoft MVP said:
Write the following line into your web.config file in system.web:

<!--- system.web goes here -->
<httpRuntime enable="false" />

The code is like this:

Configuration config = OpenWebConfiguration(ApplicationPath);
HttpRuntimeSection httpRuntimeSection = (HttpRuntimeSection)
config.GetSection("system.web/httpRuntime");
bool httpRuntimeSectionEnabled = httpRuntimeSection.Enable;

httpRuntimeSection.Enable = !httpRuntimeSectionEnabled;
SaveConfig(config);

public void SaveConfig(Configuration config) {
RemotingManager.ShutdownTargetApplication();
config.NamespaceDeclared = true;

// check if session expired
if (String.IsNullOrEmpty(ApplicationPath) ||
String.IsNullOrEmpty((string)Session[APP_PHYSICAL_PATH])) {
Server.Transfer("~/home2.aspx");
}

config.Save(ConfigurationSaveMode.Minimal);
}


Andrew said:
I have ASP.NET 2.0 application. I'm running some initialization code in
Application_Start() in Global.asax. If this code fails I would like to
shutdown the application completely. Meaning no page, or an error page is
returned to the user. I've tried HostingEnvironment.InitiateShutdown()
but it's async and still returns a page to the user. According to msdn
this method is equivalent to ApplicationManager.ShutdownApplication()
(http://msdn2.microsoft.com/en-US/li...cationmanager.shutdownapplication(VS.80).aspx).
My results are consistent with this.

Any ideas on how to programmatically shutdown an ASP.NET app are
appreciated.

Thanks,
Andrew
 
K

Ken Cox - Microsoft MVP

This is the technique Microsoft uses in their ASP.NET Web Site
Administration tool.

It seems to work well to take the site offline.

Ken

Andrew said:
Hi Ken,

Thanks for such a quick response. In the code you posted it appears as
though settings are being saved to web.config so IIS will recycle the app.
Is this correct? Is there more going on?



Ken Cox - Microsoft MVP said:
Write the following line into your web.config file in system.web:

<!--- system.web goes here -->
<httpRuntime enable="false" />

The code is like this:

Configuration config = OpenWebConfiguration(ApplicationPath);
HttpRuntimeSection httpRuntimeSection = (HttpRuntimeSection)
config.GetSection("system.web/httpRuntime");
bool httpRuntimeSectionEnabled = httpRuntimeSection.Enable;

httpRuntimeSection.Enable = !httpRuntimeSectionEnabled;
SaveConfig(config);

public void SaveConfig(Configuration config) {
RemotingManager.ShutdownTargetApplication();
config.NamespaceDeclared = true;

// check if session expired
if (String.IsNullOrEmpty(ApplicationPath) ||
String.IsNullOrEmpty((string)Session[APP_PHYSICAL_PATH])) {
Server.Transfer("~/home2.aspx");
}

config.Save(ConfigurationSaveMode.Minimal);
}


Andrew said:
I have ASP.NET 2.0 application. I'm running some initialization code in
Application_Start() in Global.asax. If this code fails I would like to
shutdown the application completely. Meaning no page, or an error page
is returned to the user. I've tried
HostingEnvironment.InitiateShutdown() but it's async and still returns a
page to the user. According to msdn this method is equivalent to
ApplicationManager.ShutdownApplication()
(http://msdn2.microsoft.com/en-US/li...cationmanager.shutdownapplication(VS.80).aspx).
My results are consistent with this.

Any ideas on how to programmatically shutdown an ASP.NET app are
appreciated.

Thanks,
Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top