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.提升性能----java 无锁结构(CAS, Atomic, Threadlocal, volatile, 函数式编码, 不变对象)
paip.提升性能----java 无锁结构(CAS, Atomic, Threadlocal, volatile, 函数式编码, 不变对象) 1 锁的缺点 2 CAS(Compare ...
- JSTL核心标签库使用
JSTL是什么? java standard taglib(java标准标签库). 标签是什么? sun公司制订的一种技术规范,用来将jsp页面中的java代码使用类似于html当中的标记来替换 的一 ...
- SQL Server 内存中OLTP内部机制概述(一)
----------------------------我是分割线------------------------------- 本文翻译自微软白皮书<SQL Server In-Memory ...
- DotNet中人民币符号的输出
DotNet中人民币符号“¥”的输出<html> <head>DotNet中人民币符号的输出</head> <body> <p>¥100元& ...
- const成员函数
尽管函数名和参数列表都相同,void foo( ) const成员函数是可以与void foo( )并存的,可以形成重载! 我们假设调用语句为obj.foo(),如果obj为non-const对象,则 ...
- Activity后台运行一段时间回来crash问题的分析与解决
最近做项目的时候碰到一个棘手的问题,花了不少时间才找到原因并解决.特此记录这个被我踩过的坑,希望其他朋友遇到此问题不要调到这坑里去了. 问题描述: 1.背景:我的app中某个界面的Activity是继 ...
- 【micropython】用python来进行BadUSB的USB-HID测试(含无线控制)
转载请注明:@小五义http://www.cnblogs.com/xiaowuyiQQ群:64770604 本文以TPYBoardv101开发板为例讲解了利用micropython进行BadUSB的u ...
- [转] Python 代码性能优化技巧
选择了脚本语言就要忍受其速度,这句话在某种程度上说明了 python 作为脚本的一个不足之处,那就是执行效率和性能不够理想,特别是在 performance 较差的机器上,因此有必要进行一定的代码优化 ...
- A Brief History of Scaling LinkedIn
原文地址 LinkedIn started in 2003 with the goal of connecting to your network for better job opportuniti ...
- jackson readTree
String jsonstr = "{\"msg\":{\"head\":{\"version\":\"1.0\&quo ...