博客
关于我
使用Topshelf 5步创建Windows 服务
阅读量:450 次
发布时间:2019-03-06

本文共 3489 字,大约阅读时间需要 11 分钟。

使用Topshelf创建Windows服务:一篇详细指南

Topshelf是一款开源的跨平台宿主服务框架,支持Windows和Mono环境。它使得开发和部署Windows服务变得异常简单,只需几行代码即可构建一个高效便捷的服务宿主。以下将详细介绍如何使用Topshelf创建Windows服务。

1. 获取Topshelf源码

Topshelf的代码托管在GitHub上,可以在GitHub Repository下载到最新版本的源码。这里你可以找到最新的开发版本和详细的文档资料。

2. 在Visual Studio中创建项目

首先,我们需要创建一个新的控制台应用程序来托管Topshelf服务。在Visual Studio中:

  • 创建新项目:选择“新建项目”,然后选择“控制台应用程序”模板。
  • 添加引用:在项目中添加引用,包括TopShelf.dlllog4net.dll。这两个文件可以从Topshelf的GitHub仓库或NuGet包中获取。
  • 配置依赖项:确保项目中已经包含了必要的NuGet包。如果没有,可以通过“引用”菜单添加NuGet包。
  • 3. 实现服务逻辑

    接下来,我们需要创建一个服务类,实现启动和停止服务的基本功能。以下是一个简单的示例代码:

    using System;using System.Timers;using log4net;namespace SampleWindowsService{    public class SampleService    {        private Timer _timer = null;        private readonly ILog _log = LogManager.GetLogger(typeof(SampleService));        public SampleService()        {            _timer = new Timer(5000);            _timer.Elapsed += new ElapsedEventHandler(OnTick);        }        protected virtual void OnTick(object sender, ElapsedEventArgs e)        {            _log.Debug("Tick: " + DateTime.Now.ToLongTimeString());        }        public void Start()        {            _log.Info("SampleService is Started");            _timer.AutoReset = true;            _timer.Enabled = true;            _timer.Start();        }        public void Stop()        {            _log.Info("SampleService is Stopped");            _timer.AutoReset = false;            _timer.Enabled = false;        }    }}

    4. 使用Topshelf配置服务

    在主方法中,我们需要使用Topshelf的宿主功能来配置和运行我们的服务。以下是一个详细的配置示例:

    using System.IO;using log4net.Config;using Topshelf;namespace SampleWindowsService{    class Program    {        static void Main(string[] args)        {            XmlConfigurator.ConfigureAndWatch(new FileInfo(".\\log4net.config"));            var host = HostFactory.New(x =>            {                x.EnableDashboard();                x.Service
    (s => { s.SetServiceName("SampleService"); s.ConstructUsing(name => new SampleService()); s.WhenStarted(tc => { XmlConfigurator.ConfigureAndWatch(new FileInfo(".\\log4net.config")); tc.Start(); }); s.WhenStopped(tc => { tc.Stop(); }); }); }); x.RunAsLocalSystem(); x.SetDescription("SampleService Description"); x.SetDisplayName("SampleService"); x.SetServiceName("SampleService"); } }}

    5. 服务配置与运行

    完成上述步骤后,你已经成功配置了一个Topshelf宿主服务。接下来,可以按照以下步骤运行和管理服务:

    • 安装服务:运行以下命令安装服务:

      SampleWindowsService.exe install
    • 启动服务:通过以下命令启动服务:

      SampleWindowsService.exe start
    • 停止服务:使用以下命令停止服务:

      SampleWindowsService.exe stop
    • 卸载服务:如果需要卸载服务,可以运行:

      SampleWindowsService.exe uninstall

    6. 日志管理

    在配置log4net时,可以通过提供的log4net.config文件定义日志级别和输出格式。例如,配置文件可能包含以下内容:

    7. 服务管理与控制台

    安装完成后,你可以通过以下命令直接控制服务的运行状态:

    • 启动服务SampleWindowsService.exe start
    • 停止服务SampleWindowsService.exe stop
    • 重启服务SampleWindowsService.exe restart
    • 卸载服务SampleWindowsService.exe uninstall

    通过这些简单的命令,你可以轻松地管理和控制你的Windows服务。

    8. 服务的卸载与重新安装

    如果你需要卸载服务,可以使用以下命令:

    SampleWindowsService.exe uninstall

    卸载完成后,你可以重新安装服务,重新获取最新版本的服务配置和代码。

    总结

    通过以上步骤,你已经掌握了使用Topshelf创建和管理Windows服务的基本方法。Topshelf提供了一种简单易用的方式来创建和管理跨平台的服务宿主,减少了传统方式中繁琐的配置和管理流程。希望这篇指南能为你提供实用的参考和帮助。

    转载地址:http://oaifz.baihongyu.com/

    你可能感兴趣的文章
    object detection错误Message type "object_detection.protos.SsdFeatureExtractor" has no field named "bat
    查看>>
    object detection错误之Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
    查看>>
    object detection错误之no module named nets
    查看>>
    Object of type 'ndarray' is not JSON serializable
    查看>>
    Object Oriented Programming in JavaScript
    查看>>
    object references an unsaved transient instance - save the transient instance before flushing
    查看>>
    Object.keys()的详解和用法
    查看>>
    OBJECTIVE C (XCODE) 绘图功能简介(转载)
    查看>>
    Objective-C ---JSON 解析 和 KVC
    查看>>
    Objective-C 编码规范
    查看>>
    Objective-C——判断对象等同性
    查看>>
    Objective-C之成魔之路【7-类、对象和方法】
    查看>>
    Objective-C享元模式(Flyweight)
    查看>>
    Objective-C以递归的方式实现二叉搜索树算法(附完整源码)
    查看>>
    Objective-C内存管理教程和原理剖析(三)
    查看>>
    Objective-C实现 Greedy Best First Search最佳优先搜索算法(附完整源码)
    查看>>
    Objective-C实现 jugglerSequence杂耍者序列算法 (附完整源码)
    查看>>
    Objective-C实现1000 位斐波那契数算法(附完整源码)
    查看>>
    Objective-C实现2 个数字之间的算术几何平均值算法(附完整源码)
    查看>>
    Objective-C实现2d 表面渲染 3d 点算法(附完整源码)
    查看>>