使用TopShelf轻松开发Window服务
关于TopShelf 描述:
Topshelf is a framework for hosting services written using the .NET framework. The creation of services is simplified, allowing developers to create a simple console application that can be installed as a service using Topshelf. The reason for this is simple: It is far easier to debug a console application than a service. And once the application is tested and ready for production, Topshelf makes it easy to install the application as a service.
1、ok,首先使用VS 建立一个控制台 程序,使用Nuget 搜索 TopShelf 引用 TopShelf 和 TopShelf.Log4Net
2、写入代码:
public class TownCrier
{
readonly Timer _timer;
public TownCrier()
{
_timer = new Timer() {AutoReset = true};
_timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} an all is well", DateTime.Now);
}
public void Start() { _timer.Start(); }
public void Stop() { _timer.Stop(); }
} public class Program
{
public static void Main()
{
HostFactory.Run(x =>
{
x.Service<TownCrier>(s =>
{
s.ConstructUsing(name=> new TownCrier());
s.WhenStarted(tc => tc.Start()); //指定此服务的启动函数
s.WhenStopped(tc => tc.Stop()); //指定此服务的终止函数
});
x.RunAsLocalSystem(); x.SetDescription("这是一个测试的服务 作用就是控制台输出~~"); // 服务描述
x.SetDisplayName("_Rufus_Test_Winservices"); //服务显示名字
x.SetServiceName("_Rufus_Test_winServices"); //服务名字
});
}
}
3、编译
4、找到你编译成功的exe程序 执行cmd命令 (ps: 需要管理员权限)
XXXXX install (xxxx是你编译的程序名 例如 我的为 WinServerTest install)
5、现在 查看 服务管理器 发现 _Rufus_Test_Winservices 已经注册到 里面 了,手动启动这个 服务即可。
使用TopShelf轻松开发Window服务的更多相关文章
- c# 开发window服务
http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html 安装 cmd 输入 InstallUtil.exe E:\TestApp\ ...
- .NET Window服务启动又马上停止,报错IO.FileNotFoundException
最近公司需要开发一个Window服务推送系统,读取MongoDB写入消息队列,推送到各终端平台 但是在开发完成,最后的部署阶段,选中服务右击启动 看似正常,服务显示已启动(但实质已经被终止,因为Win ...
- 使用Topshelf 开发windows服务
在业务系统中,我们为了调度一些自动执行的任务或从队列中消费一些消息,所以基本上都会涉及到后台服务的开发.如果用windows service开发,非常不爽的一件事就是:调试相对麻烦,而且你还需要了解 ...
- 使用Topshelf开发Windows服务、log4net记录日志
开发windows服务,除了在vs里新建服务项目外(之前有写过具体开发方法,可点击查看),还可以使用Topshelf. 不过使用topshelf需要.netframework 4.5.2版本,在vs2 ...
- topshelf 开发windows 服务资料
官方配置 http://docs.topshelf-project.com/en/latest/configuration/config_api.html#service-start-modes to ...
- C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
C# DateTime的11种构造函数 别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...
- 通过TopShelf快速开发服务程序
我之前在文章中介绍过使用NSSM将exe封装为服务,这种方式我个人是比较喜欢的,一来原始文件不受服务的开发约束,二来也可以提供简单的日志系统.线程守护等功能,是我个人比较倾向的行为.但是,有的场景下, ...
- VS轻松开发Node.js应用
PTVS开发团队又开发出一款可以在VS里编写Node.js应用程序的插件--NTVS(Node.js Tools for Visual Studio),开发者可以在VS里轻松开发Node.js应用. ...
- C# 编写Window服务基础(一)
一.Windows服务介绍: Windows服务以前被称作NT服务,是一些运行在Windows NT.Windows 2000和Windows XP等操作系统下用户环境以外的程序.在以前,编写Wind ...
随机推荐
- redhat--nagios插件--check_traffic.sh
****在被监控主机安装nrpe**** (1)在被监控主机上,增加用户和密码 useradd nagios passwd nagios (2)安装nagios插件 tar zxf nagios-pl ...
- CPC23-4 K.喵喵的神·数
题意:给出整数T,P,求c(T,P) mod P. 解法:用卢卡斯定理. 卢卡斯定理:解决c(n,m) mod p问题.Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p) ...
- [Everyday Mathematics]20150106
(1). 设 $f\in C[0,T]$, $g$ 是 $T$-周期函数, 试证: $$\bex \vlm{n}\int_0^T f(x)g(nx)\rd x=\frac{1}{T}\int_0^T ...
- sql test
1.用户表 找出id=2及他的朋友 select * from user t where id=2 or t.id=(select friend from user where id=2); sele ...
- HDU5045-Contest(状压dp)
题意: 有n个学生,m道题,给出每个同学解出m个问题的概率,在解题过程中每个学生的解题数的差不大于1,求最大能解出题目数的期望 分析: n很小,知道用状压,但是比赛没做出来(脑子太死了,有一个限制条件 ...
- hdu 2594-Simpsons’ Hidden Talents(KMP)
题意: 给你两个串a,b,求既是a的前缀又是b的后缀的最长子串的长度. 分析: 很自然的想到把两个串连接起来,根据KMP的性质求即可 #include <map> #include < ...
- hdu1896 bjfu1268 水题
很简单的模拟,我是用的优先队列.不多说,上代码(这是bjfuoj的,hdu的要稍改一下): /* * Author : ben */ #include <cstdio> #include ...
- CSS基础知识—【结构、层叠、视觉格式化】
结构和层叠 选择器的优先级顺序: style[内联元素]选择器>Id选择器>类选择器 属性选择器>元素选择器>通配器选择器 重要性:@important 有这个标记的属性值,优 ...
- SSH无法连接服务器
服务器版本如下: @kelWEB4:/etc# lsb_release -a LSB Version: :core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd ...
- bzoj 3732 Network(最短路+倍增 | LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3732 [题意] 给定一个无向图,处理若干询问:uv路径上最长的边最小是多少? [思路一 ...