日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Asp.NET 在Web頁面中管理服務(wù)

系統(tǒng) 2620 0

公司項目有一個需求,就是需要定時的給一些用戶發(fā)送郵件。本來想想蠻簡單的,只需要在Global.ascx中啟動一個定時器,然后定時執(zhí)行這項任務(wù)就好了。可是運行的結(jié)果卻不是我想想中的那樣。我寫的代碼是這樣的:

      
        private
      
      
         System.Timers.Timer m_timer;
    
      
      
        void
      
       Application_Start(
      
        object
      
      
         sender, EventArgs e)
    {
        m_timer 
      
      = 
      
        new
      
       System.Timers.Timer(
      
        60000
      
      );
      
        //
      
      
        設(shè)置間隔時間
      
      
        m_timer.Enabled = 
      
        true
      
      
        ;
        m_timer.Elapsed 
      
      += 
      
        new
      
      
         System.Timers.ElapsedEventHandler(run);
        
      
      
        //
      
      
        開啟時鐘。
      
      
                m_timer.Start();
    }
      
    

原來這個定時器只有在網(wǎng)頁第一次被訪問的時候才能啟動,就是說網(wǎng)站在被重啟的時候還需要手動的訪問我的任意一個頁面來激活這個Timer,這就罷了吧,反正去重啟的人也是我,自己注意點也就是了??墒墙?jīng)過幾天的測試,發(fā)現(xiàn)這個定時器并沒有一直執(zhí)行,到一定的時間,這個定時器就停止工作了。后來百度了一下,發(fā)現(xiàn)一種說法,就是當(dāng)應(yīng)用程序池被回收后,我們還需要再次激活這個Timer,而IIS默認(rèn)的回收時間是29小時。算了,我也忍了,畢竟這個站是一直會被訪問的。重復(fù)激活就重復(fù)激活吧,只要程序能用,咱就將就一下咯。

又經(jīng)過幾天的測試,還是發(fā)現(xiàn)Timer有事會停止工作,百度了一下,有人說是這樣的一個Timer對象,在對象池里面是沒有引用的。當(dāng)垃圾回收機制啟動的時候,這個Timer也會被回收掉,不管你有沒有在RUN??吹竭@里我崩潰了,一個簡單的定時器遇到了這么多的問題,算了我們還是不用定時器,改用服務(wù)吧,這總該可以了吧,百度了一下,發(fā)現(xiàn)了一個管理服務(wù)的類,寫了一個測試頁面測試了一下,效果還不錯

測試頁面代碼:

      
        protected
      
      
        void
      
       Page_Load(
      
        object
      
      
         sender, EventArgs e)
    {
        
      
      
        string
      
       serverName = 
      
        "
      
      
        Monitoring
      
      
        "
      
      
        ;
        
      
      
        string
      
       filePath = 
      
        "
      
      
        C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2010\\Projects\\ServiceHelloWorld\\ServiceHelloWorld\\bin\\Debug\\ServiceHelloWorld.exe
      
      
        "
      
      
        ;
        
      
      
        bool
      
       serviceIsExist =
      
         ServerHelper.DAL.ServiceHelper.ServiceExist(serverName);
        
      
      
        if
      
      
         (serviceIsExist)
        {
            Common.Common.LogManager.GetInstance().Debug(
      
      
        "
      
      
        卸載
      
      
        "
      
      
        );
            ServerHelper.DAL.ServiceHelper.UnInstallService(filePath, serverName);
            Response.Write(
      
      
        "
      
      
        卸載
      
      
        "
      
      
        );
        }
        
      
      
        else
      
      
        
        {
            
      
      
        //
      
      
          string serviceName = "";
      
      
            Common.Common.LogManager.GetInstance().Debug(
      
        "
      
      
        安裝
      
      
        "
      
      
        );
            ServerHelper.DAL.ServiceHelper.InstallService(filePath,serverName);
            Response.Write(serverName);
            Response.Write(
      
      
        "
      
      
        安裝
      
      
        "
      
      
        );
        }
      
    

安裝服務(wù)的代碼:

      
                ///
      
      
        <summary>
      
      
        ///
      
      
         安裝服務(wù)
        
      
      
        ///
      
      
        </summary>
      
      
        ///
      
      
        <param name="stateSaver"></param>
      
      
        ///
      
      
        <param name="filepath"></param>
      
      
        ///
      
      
        <param name="serviceName"></param>
      
      
        public
      
      
        static
      
      
        void
      
       InstallService(
      
        string
      
       filepath, 
      
        string
      
      
         serviceName)
        {
            System.ServiceProcess.ServiceController service 
      
      = 
      
        new
      
      
         System.ServiceProcess.ServiceController(serviceName);
            
      
      
        if
      
       (!
      
        ServiceExist(serviceName))
            {
                AssemblyInstaller myAssemblyInstaller 
      
      = 
      
        new
      
      
         AssemblyInstaller();
                myAssemblyInstaller.UseNewContext 
      
      = 
      
        true
      
      
        ;
                myAssemblyInstaller.Path 
      
      =
      
         filepath;
                IDictionary stateSaver 
      
      = 
      
        new
      
      
         Hashtable();
                stateSaver.Clear();
                myAssemblyInstaller.Install(stateSaver);
                myAssemblyInstaller.Commit(stateSaver);
                myAssemblyInstaller.Dispose();
                service.Start();
            }
            
      
      
        else
      
      
        
            {
                
      
      
        if
      
       (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status !=
      
         System.ServiceProcess.ServiceControllerStatus.StartPending)
                {
                    service.Start();
                }
            }
        }
      
    

Asp.NET 在Web頁面中管理服務(wù)

卸載服務(wù)的代碼:

      
                ///
      
      
        <summary>
      
      
        ///
      
      
         卸載服務(wù)
        
      
      
        ///
      
      
        </summary>
      
      
        ///
      
      
        <param name="filepath"></param>
      
      
        ///
      
      
        <param name="serviceName"></param>
      
      
        public
      
      
        static
      
      
        void
      
       UnInstallService(
      
        string
      
       filepath, 
      
        string
      
      
         serviceName)
        {
            
      
      
        try
      
      
        
            {
                
      
      
        if
      
      
         (ServiceExist(serviceName))
                {
                    AssemblyInstaller myAssemblyInstaller 
      
      = 
      
        new
      
      
         AssemblyInstaller();
                    myAssemblyInstaller.UseNewContext 
      
      = 
      
        true
      
      
        ;
                    myAssemblyInstaller.Path 
      
      =
      
         filepath;
                    myAssemblyInstaller.Uninstall(
      
      
        null
      
      
        );
                    myAssemblyInstaller.Dispose();
                }
            }
            
      
      
        catch
      
      
         (Exception ex)
            {
                
      
      
        throw
      
      
        new
      
       Exception(
      
        "
      
      
        unInstallServiceError/n
      
      
        "
      
       +
      
         ex.Message);
            }
        }
      
    

Asp.NET 在Web頁面中管理服務(wù)

有了這樣的一個方法,我就再也不會用Timer的方式來處理定時的事務(wù)了,而且據(jù)說大牛們都不用Timer來做事務(wù),都用服務(wù)。

Asp.NET 在Web頁面中管理服務(wù)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 灵寿县| 舒城县| 湘阴县| 保康县| 延川县| 和静县| 深水埗区| 新泰市| 南昌县| 鄂托克前旗| 乌什县| 新河县| 商南县| 闽侯县| 龙陵县| 曲沃县| 嘉黎县| 丰镇市| 巴塘县| 体育| 福安市| 金阳县| 上林县| 卢湾区| 呼玛县| 阳朔县| 当阳市| 南昌市| 邳州市| 高碑店市| 永康市| 耿马| 仲巴县| 廉江市| 鲁山县| 南开区| 望谟县| 彭泽县| 碌曲县| 宜都市| 阿拉善左旗|