天天看点

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; });
        }
    }


}