经过多次的实验,要让webservice自动运行且不被服务器杀死如果用Timer是不行的,如果用Timer在我的服务器上最多两个周就被杀死,所以每隔两个周都要去重新开一遍。最后找到了一个方法。
在Global.asax.cs 中有如下代码。
using System;
using System.Net;
using System.Web;
using System.Web.Caching;
namespace IDCServiceSutInfo
{
public class Global : System.Web.HttpApplication
{
private const string DummyCacheItemKey = "GagaGuguGigi";
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
RegisterCacheEntry();
}
private bool RegisterCacheEntry() //在缓存过期时执行回掉
{
if (null != HttpContext.Current.Cache[DummyCacheItemKey]) return false;
HttpContext.Current.Cache.Add(DummyCacheItemKey, "Test", null,
DateTime.MaxValue, TimeSpan.FromMinutes(3),
CacheItemPriority.Normal,
new CacheItemRemovedCallback(CacheItemRemovedCallback));
return true;
}
public void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
HitPage(); //模拟用户点击网页 // Do the service works
localhost.AutoServiceCaptureFailure a = new localhost.AutoServiceCaptureFailure(); //引用的本地需要长期定时执行的webservice
a.CaptureFailure4IDCDB();
}
private const string DummyPageUrl = "http://localhost/IDCServiceSutInfo/AutoServiceCaptureFailure.asmx?op=CaptureFailure4IDCDB"; //放在服务器上需要长期定时执行的webservice的链接
private void HitPage()
{
WebClient client = new WebClient();
client.DownloadData(DummyPageUrl);
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString() == DummyPageUrl)
{
RegisterCacheEntry();
}
}
}
}
web.config:
<?xml version="1.0"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="IDCServiceSutInfo.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
</configSections>
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
<compilation targetFramework="4.0" debug="true"/>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/"/>
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/"/>
</providers>
</roleManager>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
</handlers>
</system.webServer>
<applicationSettings>
<IDCServiceSutInfo.Properties.Settings>
<setting name="IDCServiceSutInfo_localhost_AutoServiceCaptureFailure"
serializeAs="String">
<value>http://localhost/IDCServiceSutInfo/AutoServiceCaptureFailure.asmx</value>
</setting>
<setting name="IDCServiceSutInfo_xmnpwmfgtest202_IDCServiceAI_ServiceEmrRecommendation"
serializeAs="String">
<value>http://xmnpwmfgtest202.xmn.apac.dell.com/idcserviceai/ServiceEmrRecommendation.asmx</value>
</setting>
</IDCServiceSutInfo.Properties.Settings>
</applicationSettings>
</configuration>
最后将项目放在服务器的IIS上,v4.0 classic的程序池中