亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Asp.NET 在Web頁面中管理服務

系統 2605 0

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

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

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

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

測試頁面代碼:

      
        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(
      
      
        "
      
      
        安裝
      
      
        "
      
      
        );
        }
      
    

安裝服務的代碼:

      
                ///
      
      
        <summary>
      
      
        ///
      
      
         安裝服務
        
      
      
        ///
      
      
        </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頁面中管理服務

卸載服務的代碼:

      
                ///
      
      
        <summary>
      
      
        ///
      
      
         卸載服務
        
      
      
        ///
      
      
        </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頁面中管理服務

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

Asp.NET 在Web頁面中管理服務


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 性视频一区二区三区免费 | 欧美人与动人物a级网站 | 亚洲精品专区一区二区三区 | 日韩免费一级片 | 99视频精品国在线视频艾草 | 香蕉九九 | 亚洲欧美综合 | 韩国日本一级毛片免费视频 | 亚洲精品一区二区伦理 | 国产精品高清免费网站 | 女人18免费毛片视频 | 亚洲国产精品一区二区久久 | 久操精品视频 | 欧美成人在线免费观看 | 91中文字幕在线播放 | 四虎黄色网址 | 午夜激情男女 | 综合色网站 | 亚洲精品国产经典一区二区 | 涩涩97在线观看视频 | 免费看欧美一级特黄α大片 | 久久久久久久国产免费看 | 精品久久久久久久久久香蕉 | 国产精品伦理一区二区三区 | 在线成人亚洲 | 欧美交换乱理伦片在线观看 | 久久久久久久国产a∨ | 五月天亚洲| 国产精品久久久久这里只有精品 | 亚洲精品免费在线视频 | 女人十八一级毛片 | 欧美日韩不卡中文字幕在线 | 91婷婷色涩涩 | 日韩欧美一二区 | 5151四虎永久在线精品免费 | 午夜一级影院 | 久久久久国产视频 | 在线视频免费观看a毛片 | 欧美成人性videos | 欧美一级级a在线观看 | 狠狠综合久久综合网站 |