Topshelf的使用
一、简介
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命令,管理服务的命令如下:
启动服务:


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

四、示例代码
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的使用的更多相关文章
- Topshelf 支持Mono 扩展Topshelf.Linux
使用Topshelf 5步创建Windows 服务 这篇文章大家可以了解到使用Topshelf可以很好的支持Windows服务的开发,但是它和Mono不兼容,Github上有一个扩展https://g ...
- Topshelf 学习 跨平台
Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主. 官网:http://topshelf-project.com Git ...
- topshelf和quartz内部分享
阅读目录: 介绍 基础用法 调试及安装 可选配置 多实例支持及相关资料 quartz.net 上月在公司内部的一次分享,现把PPT及部分交流内容整理成博客. 介绍 topshelf是创建windows ...
- 使用topshelf包装redis为windows服务
Redis服务端目前用的是控制台程序运行,部署的时候能作为windows服务后台运行感觉更好.找到一篇文章Running Redis as a Windows Service,利用win ...
- TopShelf框架创建Windows服务作为Remoting的宿主案例:
1.创建服务 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...
- 使用Topshelf快速搭建Windows服务
1.创建控制台程序 2.安装Topshelf组件 Install-Package Topshelf using System; using System.Timers; using Topshelf ...
- Topshelf + ServiceModelEx + Nlog 从头构建WCF
前言 Topshelf可以很方便的构建windows service,而且在本地开发时也可以构建Console宿主,因此很方便WCF的开发. ServiceModelEx则提供了很多便利的方法来配置w ...
- topshelf包装redis为windows服务
topshelf包装redis为windows服务 Redis服务端目前用的是控制台程序运行,部署的时候能作为windows服务后台运行感觉更好.找到一篇文章Running Redis as a Wi ...
- 使用Topshelf创建Windows服务
概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...
- 简单实现Windows服务 TopShelf
Nugut安装 log4net 和 topShelf 1)ServiceRunner类 using log4net;using Topshelf; class ServiceRunner : Serv ...
随机推荐
- Want To Say Something
2019.3.3 明天要演讲了,在这里为自己打一下气! 加油! 2019.3.31 停课三周的第一次写日志 怎么说这三周结交了很多八班的朋友 在竞赛上一直在学数论快学吐了,但没办法呀还是要为出 ...
- logstash的output配置中指定elasticsearch的template
转自:https://blog.csdn.net/felix_yujing/article/details/78930389 之前采用的是通过filebeat收集nginx的日志,直接到elastic ...
- Unity 子弹移动以及碰撞条件
一.子弹移动 游戏物体移动最主要的是获取一个刚体组件,再对这个刚体组件添加一个向前的力: 具体代码: public class BulletCtrl : MonoBehaviour { ; publi ...
- (模拟 打好基础)nyoj1363-计划日
1363-计划日 内存限制:256MB 时间限制:3000ms 特判: No通过数:21 提交数:79 难度:1 题目描述: 为什么花那么多时间.精力还是学不好学不通,如何把握各科目的重难点,期中和期 ...
- Linux工具安装和常用配置
1 常用开发工具安装 1 安装Mysql ①基本安装 wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm: s ...
- airflow1.10时区修改
https://blog.csdn.net/crazy__hope/article/details/83688986
- Gradle创建项目(IntelliJ IDEA)
创建Gradle项目 步骤一: 步骤二: 步骤三: 步骤四: 步骤五: 此时, 项目已经建好, 如果是第一次使用, 或者本地没有该版本的Gradle时, 就会触发下载.如图所示. 点击红色方框中标识的 ...
- file 自定义上传附件并展示缩略图
效果图镇楼.. 写的有点乱.上传一个实例供大家参考--附件下载地址如何下: https://files.cnblogs.com/files/fchx91/uploadFiles.rar 2019- ...
- Ext.net MessageBox提示
Ext.MessageBox.confirm("选择全部", "确定选择?", function (btn) { if (btn !== "yes&q ...
- IntelliJ IDEA 2018最新版注册码激活方法
一.首先点击intellij idea 2018 二.选择激活码 三.输入以下激活码intellij idea 2018 最新版本 注册激活码 **************************** ...