Windows服務

因工作中遇到window服務的業務特瞭解了下window服務開發,安裝,卸載,調試等,特記錄一下作爲以後回顧使用

工作業務場景:

項目中數據調用通過thrift,在調用前需要開啓多個服務,這些服務有exe,bat等,每次調用都要手動開啓很多服務,比較繁瑣;

爲了便於接口調用調試製作一個進程封裝(將所有待啓用的服務封裝起來,通過進程統一啓用,並對進程進行守護),另外可能存在服務器重啓宕機的情況,

所以要將應用做成window服務

 

1, 創建Windows服務

 

 

2.創建好Windows服務後在左側設計試圖中單擊右鍵,點擊“添加安裝程序”,選擇serviceInstaller1右鍵屬性,對其屬性做如下設置:

 

 

3.選中serviceProcessInstall1右鍵屬性,將Account屬性值設置成LocalSystem

 

4.在ProjectInstaller中重寫Commit方法

  public override void Commit(IDictionary savedState)

        {

            base.Commit(savedState);

            ServiceController sc = new ServiceController("NUCBoxService");

            if (sc.Status.Equals(ServiceControllerStatus.Stopped))

            {

                sc.Start();

            }

        }

 

5.Program中有兩個方法 OnStart  OnStop是服務啓動和停止要執行的方法

 在對應方法中編寫對應的邏輯處理

         protected override void OnStart(string[] args)

        {

            dispatcher.Init();

            dispatcher.StartAll();

            logger.Info("Start switch service");

        }

 

        protected override void OnStop()

        {

            dispatcher.StopAll();

            dispatcher.Dispose();

            logger.Info("Stop switch service");

        }

6.至此服務創建成功,服務的安裝,卸載,調試 見如下技術貼:

 

https://blog.csdn.net/sqqyq/article/details/50475437

 

https://www.cnblogs.com/knowledgesea/p/3616127.html

 

感謝上述兩篇技術貼作者分享

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章