一、简介

Topshelf可用于创建和管理Windows服务。其优势在于不需要创建windows服务,创建控制台程序就可以。便于调试。

二、官方地址:

1、官网:http://topshelf-project.com/

2、官方文档:https://topshelf.readthedocs.io/en/latest/

3、github地址:https://github.com/Topshelf/Topshelf

三、详细示例

1、创建控制台程序“Topshelf测试”,Nuget中引入Topshelf,如图所示:

也可以用Nuget的命令行引入,命令如下:

  • Install-Package Topshelf

2、在项目中添加类,命名为:TopshelfTest

内容如下:

    public class TopshelfTest
{
readonly Timer _timer;
public TopshelfTest()
{
_timer = new Timer() { AutoReset = true };
_timer.Elapsed += (sender, eventArgs) => { Run(); };
}
public void Start() { _timer.Start(); }
public void Stop() { _timer.Stop(); }
public static void Run()
{
Console.WriteLine("hello Topshelf");
}
}

3、在Main函数中加入如下代码:

        public static void Main(string[] args)
{
HostFactory.Run(x=>
{
x.RunAsLocalSystem();
x.SetDescription("topshelf测试");
x.SetDisplayName("topshelftest");
x.SetServiceName("topshelftest"); x.Service<TopshelfTest>(s =>
{
s.ConstructUsing(name => new TopshelfTest());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
});
}

4、运行程序,输出如下,每秒中会执行一次Run函数,也就会打印一次“hello Topshelf”

5、安装服务:

以管理员权限打开cmd命令,管理服务的命令如下:

安装:Topshelf测试.exe install
启动:Topshelf测试.exe start
卸载:Topshelf测试.exe uninstall
 
执行命令需要切入到程序目录下,如图所示:
安装服务:

启动服务:

 
卸载服务:

安装后可以看到服务中出现了安装的服务:

四、示例代码

using System;
using System.Timers;
using Topshelf;
namespace Topshelf测试
{
public class Program
{
public static void Main(string[] args)
{
HostFactory.Run(x=>
{
x.RunAsLocalSystem();
x.SetDescription("topshelf测试");
x.SetDisplayName("topshelftest");
x.SetServiceName("topshelftest"); x.Service<TopshelfTest>(s =>
{
s.ConstructUsing(name => new TopshelfTest());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
});
}
}
public class TopshelfTest
{
readonly Timer _timer;
public TopshelfTest()
{
_timer = new Timer() { AutoReset = true };
_timer.Elapsed += (sender, eventArgs) => { Run(); };
}
public void Start() { _timer.Start(); }
public void Stop() { _timer.Stop(); }
public static void Run()
{
Console.WriteLine("hello Topshelf");
}
}
}

Topshelf的使用的更多相关文章

  1. Topshelf 支持Mono 扩展Topshelf.Linux

    使用Topshelf 5步创建Windows 服务 这篇文章大家可以了解到使用Topshelf可以很好的支持Windows服务的开发,但是它和Mono不兼容,Github上有一个扩展https://g ...

  2. Topshelf 学习 跨平台

    Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主. 官网:http://topshelf-project.com Git ...

  3. topshelf和quartz内部分享

    阅读目录: 介绍 基础用法 调试及安装 可选配置 多实例支持及相关资料 quartz.net 上月在公司内部的一次分享,现把PPT及部分交流内容整理成博客. 介绍 topshelf是创建windows ...

  4. 使用topshelf包装redis为windows服务

           Redis服务端目前用的是控制台程序运行,部署的时候能作为windows服务后台运行感觉更好.找到一篇文章Running Redis as a Windows Service,利用win ...

  5. TopShelf框架创建Windows服务作为Remoting的宿主案例:

    1.创建服务 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  6. 使用Topshelf快速搭建Windows服务

    1.创建控制台程序 2.安装Topshelf组件  Install-Package Topshelf using System; using System.Timers; using Topshelf ...

  7. Topshelf + ServiceModelEx + Nlog 从头构建WCF

    前言 Topshelf可以很方便的构建windows service,而且在本地开发时也可以构建Console宿主,因此很方便WCF的开发. ServiceModelEx则提供了很多便利的方法来配置w ...

  8. topshelf包装redis为windows服务

    topshelf包装redis为windows服务 Redis服务端目前用的是控制台程序运行,部署的时候能作为windows服务后台运行感觉更好.找到一篇文章Running Redis as a Wi ...

  9. 使用Topshelf创建Windows服务

    概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...

  10. 简单实现Windows服务 TopShelf

    Nugut安装 log4net 和 topShelf 1)ServiceRunner类 using log4net;using Topshelf; class ServiceRunner : Serv ...

随机推荐

  1. 论Scrapy中的数据持久化

    引入 Scrapy的数据持久化,主要包括存储到数据库.文件以及内置数据存储. 那我们今天就来讲讲如何把Scrapy中的数据存储到数据库和文件当中. 终端指令存储 保证爬虫文件的parse方法中有可迭代 ...

  2. 查看Linux的所有线程

    查看Linux所有线程有3种方法: ps -T <pid>可以看指定pid的所有线程,SPID就是指线程.或者用ps -eLf top -H,和普通的top命令相比,多了Thread ht ...

  3. 清理Windows Serer Backup备份数据生成的卷影副本(DiskShadow命令)

    DiskShadow基本命令: 1.进入DiskShadow命令行: C:\>diskshadow 2.列出所有的卷影副本: DISKSHADOW> list shadows all 3. ...

  4. hadoop记录-hadoop集群日常运维命令

    hadoop集群日常运维命令 #1.namenode hadoop namenode -format #格式化,慎用 su hdfs hadoop-daemon.sh start namenode h ...

  5. 一封来自恶魔的挑战邀请函,那些你见过或者没见过的C语言指针都在这里了

    前言 相信大多数的同学都是第一门能接触到语言是C/C++,其中的指针也是比较让人头疼的部分了,因为光是指针都能专门出一本叫<C和指针>的书籍,足见指针的强大.但如果不慎误用指针,这些指针很 ...

  6. ArcGIS——2015年安徽各市GDP总量分级图(3等级)

  7. kail linux虚拟机安装tools工具

    因为自己比较懒,有时候自己不想打字需要粘贴就安装了虚拟机tools工具,又因为自己脑子不好使所以就写一下步骤,以便以后用得着.我这里用得是kail linux系统,不知道contest能不能这样安,下 ...

  8. Python 高级特性之:生成器(generator)和迭代器(Iterator)

    前言: 之前学习Python自动化,接触了不少python的学习,对生成器印象尤其深,网上也看了很多介绍,下面主要是这些概念的个人学习整理(如侵删). 正文: 如要创建一个非常大的列表,受到内存限制, ...

  9. python 数据分析工具之 numpy pandas matplotlib

    作为一个网络技术人员,机器学习是一种很有必要学习的技术,在这个数据爆炸的时代更是如此. python做数据分析,最常用以下几个库 numpy pandas matplotlib 一.Numpy库 为了 ...

  10. AXI_DMA IP学习

    参考:PG201  AXI DMA v7.1    AXI IP核 功能:一旦处理器配置好传输方式之后,DMA可以自己完成内存数据的搬进或者搬出,而不需要处理器的介入.如果使用方法得当,DMA可以显著 ...