天天看點

TopShelf-(01)控制台執行個體-Program

開發windows服務時,最麻煩的就是調試了,需要安裝=》運作=》附加程序=》開始調試。修改代碼後要重複以上步驟。

topshelf 允許使用控制台程式開發 windows服務,調試非常友善。就像調試控制台程式一樣 F5 啟動就OK了。

開發時我們可以大大節省調試時間,隻需幾行代碼就可以開發出一個windows服務。

介紹:

topshelf是建立windows服務的一種方式,相比原生實作ServiceBase、Install.Installer更為簡單友善, 

我們隻需要幾行代碼即可實作windows服務的開發。

topshelf相對原生來說,調試起來比較友善,可以在開發時以控制台的形式直接f5調試,釋出時用指令以服務的形式部署。

還一個比較有用的特性是支援多執行個體的部署,這樣可以在一台機器上部署多個相對的服務。

官網:http://topshelf-project.com/

源碼:https://github.com/Topshelf/Topshelf/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using Topshelf;
using TopshelfServiceRule;


namespace TopshelfService
{
    /*
     * Topshelf 架構安裝服務常用指令
     * 安裝:TopshelfService.exe install
     * 啟動:TopshelfService.exe start
     * 停止:TopshelfService.exe stop
     * 解除安裝:TopshelfService.exe uninstall
     */
    class Program
    {
        static void Main(string[] args)
        {
            HostFactory.Run(x =>
            {
                //使用 TownCrier 類,配置服務事件
                x.Service<TownCrier>(s =>
                {
                    //使用自定義的服務
                    s.ConstructUsing(name => new TownCrier());
                    //服務啟動事件
                    s.WhenStarted(tc => tc.Start());
                    //服務停止後事件
                    s.WhenStopped(tc => tc.Stop());
                    //服務停止後繼續運作時事件
                    s.WhenContinued(tc => tc.Continued());
                    //服務暫定事件
                    s.WhenPaused(tc => tc.Paused());
                    //服務解除安裝事件
                    s.WhenShutdown(tc => tc.Shutdown());
                });


                #region 服務配置
                
                x.RunAsLocalSystem();


                #region 啟動類型
                x.StartAutomatically();//自動運作
                //x.StartManually();//手動運作
                //x.StartAutomaticallyDelayed();//自動延遲運作
                //x.Disabled();//禁用
                #endregion
                
                
                #region 服務資訊
                x.SetDescription("111 服務 111服務的描述");//描述
                x.SetDisplayName("111 顯示名");//顯示名稱
                x.SetServiceName("111服務");//服務名稱
                #endregion


                #endregion
            });   
        }
    }
    //服務調用的類,其中的函數對應服務中的事件,如啟動事件、暫停事件、恢複事件、繼續運作事件等
    public class TownCrier
    {
        readonly Timer _timer;
        public TownCrier()
        {
            _timer = new Timer(1000) { AutoReset = true };
            _timer.Elapsed += (sender, eventArgs) =>
            {
                Console.WriteLine("It is {0} and all is well", DateTime.Now);
                Msg(string.Format("It is {0} and all is well", DateTime.Now));
            };
        }
        public void Start() { _timer.Start();Msg("已啟動");}
        public void Stop() { _timer.Stop(); Msg("已停止"); }
        public void Continued() { Msg("繼續運作"); }
        public void Paused() { Msg("已暫停"); }
        public void Shutdown() { Msg("已解除安裝"); }


        public void Msg(params string[] msg)
        {
            ServiceEvent.Msg(msg);
            if (msg != null)
                msg.All(x => { Console.WriteLine(x);return true; });
        }
    }


}