Windows 服务开发框架介绍 - Topshelf
关于 TopShelf
Topshelfis 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.
示例
第一步:新建一个 C# 控制台应用程序。
第二步:用 Nuget 引用 TopShelf 和 Log4net。
第三步:贴出下面的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers; using Topshelf;
using Topshelf.Builders;
using Topshelf.Configurators;
using Topshelf.HostConfigurators; namespace TopshelfStudy
{
public sealed class TimeReporter
{
private readonly Timer _timer;
public TimeReporter()
{
_timer = new Timer() { AutoReset = true };
_timer.Elapsed += (sender, eventArgs) => Console.WriteLine("当前时间:{0}", DateTime.Now);
}
public void Start() { _timer.Start(); }
public void Stop() { _timer.Stop(); }
} class Program
{
static void Main(string[] args)
{
HostFactory.Run(x =>
{
x.Service<TimeReporter>(s =>
{
s.ConstructUsing(settings => new TimeReporter());
s.WhenStarted(tr => tr.Start());
s.WhenStopped(tr => tr.Stop());
}); x.RunAsLocalSystem(); x.SetDescription("定时报告时间");
x.SetDisplayName("时间报告器");
x.SetServiceName("TimeReporter");
});
}
}
}
第四步:编译、Release 生成。把 Release 文件夹 Copy 到 C 盘。
第五步:以管理员身份运行 cmd,安装、启动。
SampleWindowsService.exe install
启动
SampleWindowsService.exe start
如果要卸载也很方便
SampleWindowsService.exe uninstall
效果如下:

最后 Windows Service 没有 Console.WriteLine,我们可以把输出信息输出到文件。
StreamWriter sw = new StreamWriter("e:\\temp\\log.log");
sw.AutoFlush = true;
Console.SetOut(sw);
参考网站:
http://www.cnblogs.com/shanyou/archive/2011/05/04/2037008.html
http://www.cnblogs.com/happyframework/p/3601995.html
http://www.cnblogs.com/chenxizhang/archive/2010/04/21/1716773.html
谢谢浏览!
Windows 服务开发框架介绍 - Topshelf的更多相关文章
- 一款非常好用的 Windows 服务开发框架,开源项目Topshelf
Topshelf是一个开发windows服务的比较好的框架之一,以下演示如何开发Topshelf服务. 1.首先打开你的vs.新建一个TopshelfStudy控制台程序,如下图所示: 这是我用vs2 ...
- [Solution] Microsoft Windows 服务(2) 使用Topshelf创建Windows服务
除了通过.net提供的windows服务模板外,Topshelf是创建Windows服务的另一种方法. 官网教程:http://docs.topshelf-project.com/en/latest/ ...
- .NET Core Generic Host Windows服务部署使用Topshelf
此文源于前公司在迁移项目到.NET Core的过程中,希望使用Generic Host来管理定时任务程序时,没法部署到Windows服务的问题,而且官方也没给出解决方案,只能关注一下官方issue # ...
- Topshelf:一款非常好用的 Windows 服务开发框架
背景 多数系统都会涉及到“后台服务”的开发,一般是为了调度一些自动执行的任务或从队列中消费一些消息,开发 windows service 有一点不爽的是:调试麻烦,当然你还需要知道 windows s ...
- Topshelf:一款非常好用的 Windows 服务开发框架 转发https://www.cnblogs.com/happyframework/p/3601995.html
背景 多数系统都会涉及到“后台服务”的开发,一般是为了调度一些自动执行的任务或从队列中消费一些消息,开发 windows service 有一点不爽的是:调试麻烦,当然你还需要知道 windows s ...
- 使用Topshelf组件构建简单的Windows服务
很多时候都在讨论是否需要了解一个组件或者一个语言的底层原理这个问题,其实我个人觉得,对于这个问题,每个人都有自己的看法,个人情况不同,选择的方式也就会不同了.我个人觉得无论学习什么,都应该尝试着去了解 ...
- C#开发Windows服务详细流程
1.Windows服务简单介绍 Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序,主要用于长时间运行的功能或者执行定时任务.一般情况下,用户不能通过用户界面来安装和启 ...
- Windows服务 + Quartz.NET
服务基础 安装管理员打开cmd cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 InstallUtil.exe Path_WinSvc.exe 或者 ...
- C#创建、安装一个Windows服务
关于WIndows服务的介绍,之前写过一篇: http://blog.csdn.net/yysyangyangyangshan/article/details/7295739.可能这里对如何写一个服务 ...
随机推荐
- paip.hibernate list 返回位null的解决
paip.hibernate list 返回位null的解决 但是size是对的... 里面都是null... hb3.2版本...oracle11g =========原因:没主键 也许没主键,看累 ...
- vc个版本对应的vs版本
VC6VC7(2003)VC8(2005)VC9(2008)VC10(2010)VC11(2012)VC12(2013)VC14(2015)
- ps、grep和kill联合使用杀掉进程
例如要杀掉hello这个进程,使用下面这个命令就能直接实现. ps -ef |grep hello |awk '{print $2}'|xargs kill -9 这里是输出ps -ef |gre ...
- 【转】BitKeeper与Linux,git史前琐事
http://www.path8.net/tn/archives/6039 维持数年的BitKeeper与Linux的关系最终还是落入了好莱坞明星婚姻式的结局.他们曾经相得益彰,最后却走到这个遗憾的地 ...
- Android调用浏览器打开网址遇到的问题
我自己的手机(一加一代,升级了氢OS),然后在点击游戏内一个"隐私政策"-- 需要打开一个网页,然后就crash了.出错的信息如下: 完全是看不出来,然后我单独写了一个demo来测 ...
- Radio Basics for RFID
Radio Basics for RFID The following is excerpted from Chapter 3: Radio Basics for UHF RFID from the ...
- H608B无线路由破解方法
家里的无线路由送人了,准备重新买,今日一看,竟然自带的猫(ZTE H608B)有wifi天线和4个LAN口,想着是支持路由功能的,那必然就是从软件上做了手脚.搜索该型号发现没人刷openwrt或者to ...
- Spark核心—RDD初探
本文目的 最近在使用Spark进行数据清理的相关工作,初次使用Spark时,遇到了一些挑(da)战(ken).感觉需要记录点什么,才对得起自己.下面的内容主要是关于Spark核心-RDD的相关 ...
- Android使用的设计模式1——观察者模式
设计模式,对程序员来说是一个坎,想在程序员这条路走得更远,设计模式是你的必修课.从大学时代接触GoF到工作几年后重新看设计模式,每次感觉都不一样.这次想借着分析Android Framework源码的 ...
- CKEditor4.1和CKFinder2.3.1 for Mvc4最新 破解版,结合 打造"帅"的编辑器 For .Net
CKEditor4.1+CKFinder2.3.1 FOR MVC4 最新破解版:(2013-3-23) baidu share: http://pan.baidu.com/share/link?sh ...