zoukankan      html  css  js  c++  java
  • Topshelf入门

    简介

    Topshelf允许我们快速的开发、调试和部署windows服务。

    官方网站

    使用方法

    第一步:安装

    Install-Package Topshelf

    Install-Package Topshelf.Log4Net

    虽然安装Topshelf.Log4Net不是必须的,不过建议安装。

    第二步:实现服务逻辑

    ServiceRunner.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;
    using System.IO;
    
    using Topshelf;
    using log4net;
    using log4net.Config;
    
    namespace WHTR.Demos.Topshelf
    {
        public sealed class ServiceRunner : ServiceControl, ServiceSuspend
        {
            private static ILog Logger = LogManager.GetLogger(typeof(Program));
    
            private Timer timer;
    
            static ServiceRunner()
            {
                var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
                XmlConfigurator.ConfigureAndWatch(logCfg);
            }
    
            public bool Start(HostControl hostControl)
            {
                this.timer = new Timer(new TimerCallback(this.PrintMessage), null, 1000, 1000);
    
                return true;
            }
    
            public bool Stop(HostControl hostControl)
            {
                throw new NotImplementedException();
            }
    
            public bool Continue(HostControl hostControl)
            {
                throw new NotImplementedException();
            }
    
            public bool Pause(HostControl hostControl)
            {
                throw new NotImplementedException();
            }
            private void PrintMessage(object state)
            {
                Logger.Info(DateTime.Now);
            }
        }
    }
    

    备注:要实现的接口及方法名意义非常明显,这里就不做过多说明了。

    第三步:调用

    Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using Topshelf;
    using Topshelf.ServiceConfigurators;
    
    namespace TopshelfDemos
    {
        class Program
        {
            static void Main(string[] args)
            {
                HostFactory.Run(x =>
                {
                    //x.UseLog4Net("~/log4net.config");
    
                    x.Service<ServiceRunner>();
    
                    x.SetDescription("TopshelfDemos说明");
                    x.SetDisplayName("TopshelfDemos显示名称");
                    x.SetServiceName("TopshelfDemos服务名称");
    
                    x.EnablePauseAndContinue();
                });
            }
        }
    }
    

    第四步:安装&卸载

    • 安装:TopshelfDemos.exe install
    • 卸载:TopshelfDemos.exe uninstall
    • 安装不同的实例:TopshelfDemos.exe install -instance "xxx" -servicename "xxx" -description "xxx" -displayname "xxx"
    • 卸载不同的实例 TopshelfDemos.exe uninstall -instance "xxx"

    备注

    以前以为开发Windows服务是多么高大上的东西,没想到这么简单。就两步,引用Topshelf.dll,然后实现接口,完成。

  • 相关阅读:
    Web负载均衡的几种实现方式
    Apache和Nginx的区别
    Nginx和Apache区别
    Git 使用中显示“Another git process seems to be running in this repository...”问题解决
    上传本地代码到gitHub过程详解
    MySQL数据库中varchar与char类型的区别
    正则表达式中/i,/g,/ig,/gi,/m的区别和含义
    内行看门道:看似“佛系”的《QQ炫舞手游》,背后的音频技术一点都不简单
    惧怕羊毛党?腾讯云为你保驾护航
    教你1天搭建自己的“微视”
  • 原文地址:https://www.cnblogs.com/quan2005/p/3974579.html
Copyright © 2011-2022 走看看