国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Topshelf入門

2019-11-14 16:26:27
字體:
來源:轉載
供稿:網友

簡介

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,然后實現接口,完成。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 伊宁市| 龙南县| 博罗县| 黄大仙区| 加查县| 娱乐| 云安县| 南和县| 简阳市| 固原市| 绍兴市| 永城市| 西乡县| 阿克陶县| 榆树市| 读书| 唐河县| 仙游县| 乐平市| 西乌珠穆沁旗| 信阳市| 阿巴嘎旗| 偏关县| 聂拉木县| 英山县| 鹿泉市| 梓潼县| 军事| 阿尔山市| 寿宁县| 开化县| 泉州市| 梓潼县| 西丰县| 迭部县| 丰都县| 安西县| 崇左市| 连城县| 额尔古纳市| 新余市|