ASP.net中让webservice自动运行在服务器上不被杀死

经过多次的实验,要让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的程序池中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值