.NET开发Windows Service程序 - Topshelf
在实际项目开发过程中,会经常写一些类似定时检查,应用监控的应用。这类应用在windows平台通常都会写成window service程序。
在百度上搜索一下'c#开发windows service',基本都是使用VS windows服务的模板来开发,使用VS Attach到服务的进程来调试,使用InstallUtil工具来安装windows服务。还是有些麻烦的。
本文主要介绍一下使用Topshelf开源框架来创建windows服务,简单、方便和快捷。官方文档也很简单,花半个小时过一遍即可https://topshelf.readthedocs.io/en/latest/configuration/quickstart.html
创建一个控制台程序,安装Topshelf到工程中
Install-Package Topshelf
Install-Package Topshelf.NLog // for Logging
程序示例
using System;
using System.Timers;
using Topshelf; namespace TopshelfFirstDemo
{
public class TownCrier
{
readonly Timer _timer;
public TownCrier()
{
_timer = new Timer(1000) { AutoReset = true };
_timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} and all is well", DateTime.Now);
}
public void Start() { _timer.Start(); }
public void Stop() { _timer.Stop(); }
} class Program
{
static int Main(string[] args)
{
var exitCode = HostFactory.Run(x =>
{
x.UseNLog(); x.Service<TownCrier>(s =>
{
s.ConstructUsing(name => new TownCrier());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop()); });
x.RunAsLocalSystem(); x.SetDescription("A test service using Topshelf");
x.SetDisplayName("TestTopshelf");
x.SetServiceName("TestTopshelf"); x.OnException(ex =>
{
// Logging
});
}); Console.WriteLine(exitCode); return (int)exitCode;
}
}
}
可通过设置Service各种参数来设置开发的windows service,关于Service的参数配置,请参考官方文档。
安装windows service
CD到示例程序Debug/Release bin目录
列出各命令
TopshelfFirstDemo.exe help
安装服务
TopshelfFirstDemo.exe install
卸载服务
TopshelfFirstDemo.exe uninstall
start和stop服务的方式有很多了,通过
- 到windows服务界面(cmd -> services.msc)手动启停
- net start/stop TestTopshelf
- TopshelfFirstDemo.exe start/stop
.NET开发Windows Service程序 - Topshelf的更多相关文章
- C#Windows Service程序的创建安装与卸载
C#Windows Service程序的创建安装与卸载 一.开发环境 操作系统:Windows7x64 sp1 专业版 开发环境:Visual studio 2013 编程语言:C# .NET版本: ...
- WCF Windows Service Using TopShelf and ServiceModelEx z
http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/ There are ...
- 用Delphi7开发Web Service程序 转
转:http://rosehacker.blog.51cto.com/2528968/450160 用Delphi7开发Web Service程序,并把服务程序放在IIS Web服务器上提供给 ...
- .net core 开发 Windows Forms 程序
我是一名 ASP.NET 程序员,专注于 B/S 项目开发.累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html 引言 .net cor ...
- 如何利用mono把.net windows service程序迁移到linux上
How to migrate a .NET Windows Service application to Linux using mono? 写在最前:之所以用要把windows程序迁移到Linux上 ...
- C#中级-Windows Service程序安装注意事项
一.前言 这周除了改写一些识别算法外,继续我的Socket服务编写.服务器端的Socket服务是以Windows Service的形式运行的. 在我完成Windows Service编写后,启动服务时 ...
- C#中级-通过注册表读取Windows Service程序执行路径
一.前言 假设我们的C#解决方案中有多个程序应用,如:Web应用.控制台程序.WPF程序应用和Windows服务应用. 那么这些非Windows Service应用程序怎么在代码中找到W ...
- windows service程序的Environment.CurrentDirectory路径
当前工作目录Environment.CurrentDirectory,对于winform程序,其是在程序放置的目录里, 而windows service的Environment.CurrentDire ...
- .Net 开发Windows Service
1.首先创建一个Windows Service 2.创建完成后切换到代码视图,代码中默认有OnStart和OnStop方法执行服务开启和服务停止执行的操作,下面代码是详细解释: using Syste ...
随机推荐
- 一个简单的Python爬虫
写了一个抓taobao图片的爬虫,全是用if,for,while写的,比较简陋,入门作品. 从网页http://mm.taobao.com/json/request_top_list.htm?type ...
- IDE改为AHCI后系统无法启动的解决办法
1.intel CPU 一.找到HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Msahci,“Start”项值改为“0”. 二.重启,进入b ...
- 邮箱格式验证demo
<html> <head> <script type="text/javascript"> function validate_email(fi ...
- 基于asp.net MVC 的服务器和客户端的交互(三)之客户端请求响应
一.分析 WEB API 中HTTP 请求方式的四个主要方法 (GET, PUT, POST, DELETE), 按照下列方式映射为 CURD 操作: GET 用于获取 URI 资源的进行展示,GET ...
- XML DOM 循环(foreach)读取PHP数据 和 PHP 编写 XML DOM 【转载】
用 PHP 读取和编写可扩展标记语言(XML)看起来可能有点恐怖.实际上,XML 和它的所有相关技术可能是恐怖的,但是用 PHP 读取和编写 XML 不一定是项恐怖的任务.首先,需要学习一点关于 XM ...
- mongodb c++ 驱动库编译
git clone 'https://github.com/mongodb/mongo-cxx-driver.git' scons -j2 --c++11=on --sharedclient --us ...
- jquery实现点击页面空白隐藏指定菜单
注意:dmenu是一个div的class名哦 代码如下 复制代码 $('html,body').click(function(e){ if(e.target.id.indexOf("dme ...
- HTML+CSS学习笔记 (14) - 单位和值
标签:HTML+CSS 颜色值 在网页中的颜色设置是非常重要,有字体颜色(color).背景颜色(background-color).边框颜色(border)等,设置颜色的方法也有很多种: 1.英文命 ...
- in C#,编译型常量(const)和运行时常量(readonly)
readonly 关键字与 const 关键字不同. const 字段只能在该字段的声明中初始化. readonly 字段可以在声明或构造函数中初始化. 因此,根据所使用的构造函数, readonly ...
- 数组的join()函数操作
join()函数的功能为:把数组的所有元素放入一个字符串,元素通过指定的分隔符分隔. 设置这样的数组操作 var a = []; a.push(1);a.push(3.1415926);a.push( ...