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


}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值