1 空闲20分钟后自动停止的问题
最近做一个收发短信息的webservice, 收发短信之前需要先和远程的sp服务建立连接, 于是在 void Application_Start(object sender, EventArgs e) 中与远程sp服务建立连接,在void Application_End(object sender, EventArgs e)中断开连接,同时加入了连接及断开连接的日志信息,运行过程中, 发现当网站空闲20分钟以后,就会Application_End,下次访问webservice的时候再Application_Start,很是麻烦, 查了些资料,发现是因为"应用程序池"搞的, 每个web application部署在iis上以后都会自动应用缺省的应用程序池DefaultAppPool,查看这个池的属性可以看到默认设置空闲20分钟关闭工作进程,为了不影响其他的web程序,在iis里新建一个应用程序池, 将空闲时间设置为120分钟或更长,也可以关闭这个选项, 根据需要定吧, 然后设置自己的web service使用这个新建的池.这样就不会出现空闲20分钟后Application_End的现象了.
2 强制停止Application
System.Web.HttpRuntime.UnloadAppDomain();
3 记录Application结束的原因
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
// 记录重起的原因
HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime",
BindingFlags.NonPublic
| BindingFlags.Static
| BindingFlags.GetField,
null,
null,
null);
if (runtime == null)
return;
string shutDownMessage = (string)runtime.GetType().InvokeMember("_shutDownMessage",
BindingFlags.NonPublic
| BindingFlags.Instance
| BindingFlags.GetField,
null,
runtime,
null);
string shutDownStack = (string)runtime.GetType().InvokeMember("_shutDownStack",
BindingFlags.NonPublic
| BindingFlags.Instance
| BindingFlags.GetField,
null,
runtime,
null);
if (!EventLog.SourceExists(".NET Runtime"))
{
EventLog.CreateEventSource(".NET Runtime", "Application");
}
EventLog log = new EventLog();
log.Source = ".NET Runtime";
log.WriteEntry(String.Format("/r/n/r/n_shutDownMessage={0}/r/n/r/n_shutDownStack={1}",
shutDownMessage,
shutDownStack),
EventLogEntryType.Error);
}
4 Seven reason for application restart in Asp.net 2.0
Here is a list of events when the Asp.net application restarts in Asp.Net 2.0
Adding, modifying, or deleting assemblies from the application's Bin folder.
Adding, modifying, or deleting localization resources from the App_GlobalResources or App_LocalResources folders.
Adding, modifying, or deleting the application's Global.asax file.
Adding, modifying, or deleting source code files in the App_Code directory.
Adding, modifying, or deleting Profile configuration.
Adding, modifying, or deleting Web service references in the App_WebReferences directory.
Adding, modifying, or deleting the application's Web.config file.
这些原因通过上面的方法可以在系统日志里看到具体的原因.
参考文章:
http://www.vikramlakhotia.com/Seven_reason_for_application_restart_in_Aspnet_20.aspx
http://weblogs.asp.net/scottgu/archive/2005/12/14/433194.aspx
http://bluedragon.blog-city.com/why_might_a_net_web_application_app_domain_restart_the_reaso.htm