c#寫windows服務


序言

前段時間做一個數據遷移項目,剛開始用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服務

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

點調試--->> 附加到進程

即可。

注意的是:

 

打開任務管理器:結束進程。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM