c#寫windows服務

張龍豪發表於2014-03-21

序言

前段時間做一個資料遷移專案,剛開始用B/S架構做的專案,但B/S要寄存在IIs中,而IIs又不穩定因素,如果重啟IIs就要開啟頁面才能執行專案。有不便之處,就改用Windows服務實現。這篇就總結下,windows服務的編寫,除錯,安裝解除安裝。

Windows服務介紹

Microsoft Windows 服務能夠建立在它們自己的 Windows 會話中可長時間執行的可執行應用程式。這些服務可以在計算機啟動時自動啟動,可以暫停和重新啟動而且不顯示任何使用者介面。這使服務非常適合在伺服器上使用,或任何時候,為了不影響在同一臺計算機上工作的其他使用者,需要長時間執行功能時使用。還可以在不同於登入使用者的特定使用者帳戶或預設計算機帳戶的安全上下文中執行服務。本文就向大家介紹如何運用Visual C#來一步一步建立一個檔案監視的Windows服務程式,然後介紹如何安裝、測試和除錯該Windows服務程式。

建立Windows服務

 

建立好專案之後 --- >> 雙擊 Service1.cs  ---- >>  出現一個設計介面   ---->> 右鍵介面  --- >> 彈出對話方塊選擇新增安裝程式

上面一系列操作完成後,就可以對windows服務名稱描述以及啟動方式等進行修改。

[RunInstaller(true)]

    public class Installer1 : System.Configuration.Install.Installer
    {

        /// <summary>
        /// 必需的設計器變數。
        /// </summary>
        private System.ComponentModel.Container components = null;
        private System.ServiceProcess.ServiceProcessInstaller spInstaller;
        private System.ServiceProcess.ServiceInstaller sInstaller;


        public Installer1()
        {
            // 該呼叫是設計器所必需的。
            InitializeComponent();
            // TODO: 在 InitComponent 呼叫後新增任何初始化
        }



        #region Component Designer generated code
        /// <summary>
        /// 設計器支援所需的方法 - 不要使用程式碼編輯器修改
        /// 此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            // 建立ServiceProcessInstaller物件和ServiceInstaller物件
            this.spInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            this.sInstaller = new System.ServiceProcess.ServiceInstaller();

            // 設定ServiceProcessInstaller物件的帳號、使用者名稱和密碼等資訊
            this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            this.spInstaller.Username = null;
            this.spInstaller.Password = null;


            // 設定服務名稱
            this.sInstaller.ServiceName = "PmsDataUpdateService";
            //服務描述
            this.sInstaller.Description = "hi longhao !";

            // 設定服務的啟動方式
            this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

            this.Installers.AddRange(
                new System.Configuration.Install.Installer[] { this.spInstaller, this.sInstaller });
        }

        #endregion

    }

修改好後回頭,寫入自己想要的操作。Service1.cs出現設計介面,雙擊設計介面進入cs內碼表。可以重寫這些方法。

  protected override void OnStart(string[] args)
       {
           //服務開啟執行程式碼
       }
       protected override void OnStop()
       {
           //服務結束執行程式碼
       }
       protected override void OnPause()
       {
           //服務暫停執行程式碼
           base.OnPause();
       }
       protected override void OnContinue()
       {
           //服務恢復執行程式碼
           base.OnContinue();
       }
       protected override void OnShutdown()
       {
           //系統即將關閉執行程式碼
           base.OnShutdown();
       }

除此之外還有一個Program.cs檔案:開啟看下。

使得一個Windows服務程式能夠正常執行,我們需要像建立一般應用程式那樣為它建立一個程式的入口點。在Windows服務程式中,我們也是在Main()函式中完成這個操作的。首先我們在Main()函式中建立一個Windows服務的例項,該例項應該是ServiceBase類的某個子類的物件,然後我們呼叫由基類ServiceBase類定義的一個Run()方法。然而Run()方法並不就開始了Windows服務程式,我們必須通過前面提到的服務控制管理器呼叫特定的控制功能來完成Windows服務程式的啟動,也就是要等到該物件的OnStart()方法被呼叫時服務才真正開始執行。如果你想在一個Windows服務程式中同時啟動多個服務,那麼只要在Main()函式中定義多個ServiceBae類的子類的例項物件就可以了,方法就是建立一個ServiceBase類的陣列物件,使得其中的每個物件對應於某個我們已預先定義好的服務。

 /// <summary>
        /// 應用程式的主入口點。
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new Service1(),
                new Service2() 
            };
            ServiceBase.Run(ServicesToRun);
        }

如果你在你需要的函式裡面寫過你需要的方法後,點執行則不可執行。

安裝解除安裝windows服務

1、安裝需要用,這個小玩意可以在網上下載到的。

2、把他放到你編寫好的服務程式/bin/Debug資料夾下。

3、開啟

4、用命令讀到你服務.exe資料夾下。

5、執行 installutil.exe 

6、安裝服務命令: installutil  yourservices.exe

7、解除安裝服務命令: installutil  /u  yourservices.exe

注意的是:安裝跟解除安裝需要保證程式是一樣的,沒有變更過的,要不會提示解除安裝不乾淨。也就是在已安裝過服務的時候,不要在vs中修改你的程式。

除錯windows服務

保證你的服務已安裝成功,且處於啟動模式。

點除錯--->> 附加到程式

即可。

注意的是:

 

開啟工作管理員:結束程式。

 

相關文章