在实际项目开发过程中,会经常写一些类似定时检查,应用监控的应用。这类应用在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服务的方式有很多了,通过

  1. 到windows服务界面(cmd -> services.msc)手动启停
  2. net start/stop TestTopshelf
  3. TopshelfFirstDemo.exe start/stop

.NET开发Windows Service程序 - Topshelf的更多相关文章

  1. C#Windows Service程序的创建安装与卸载

    C#Windows Service程序的创建安装与卸载 一.开发环境 操作系统:Windows7x64 sp1 专业版 开发环境:Visual studio 2013 编程语言:C# .NET版本: ...

  2. WCF Windows Service Using TopShelf and ServiceModelEx z

    http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/ There are ...

  3. 用Delphi7开发Web Service程序 转

        转:http://rosehacker.blog.51cto.com/2528968/450160 用Delphi7开发Web Service程序,并把服务程序放在IIS Web服务器上提供给 ...

  4. .net core 开发 Windows Forms 程序

    我是一名 ASP.NET 程序员,专注于 B/S 项目开发.累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html 引言 .net cor ...

  5. 如何利用mono把.net windows service程序迁移到linux上

    How to migrate a .NET Windows Service application to Linux using mono? 写在最前:之所以用要把windows程序迁移到Linux上 ...

  6. C#中级-Windows Service程序安装注意事项

    一.前言 这周除了改写一些识别算法外,继续我的Socket服务编写.服务器端的Socket服务是以Windows Service的形式运行的. 在我完成Windows Service编写后,启动服务时 ...

  7. C#中级-通过注册表读取Windows Service程序执行路径

    一.前言        假设我们的C#解决方案中有多个程序应用,如:Web应用.控制台程序.WPF程序应用和Windows服务应用. 那么这些非Windows Service应用程序怎么在代码中找到W ...

  8. windows service程序的Environment.CurrentDirectory路径

    当前工作目录Environment.CurrentDirectory,对于winform程序,其是在程序放置的目录里, 而windows service的Environment.CurrentDire ...

  9. .Net 开发Windows Service

    1.首先创建一个Windows Service 2.创建完成后切换到代码视图,代码中默认有OnStart和OnStop方法执行服务开启和服务停止执行的操作,下面代码是详细解释: using Syste ...

随机推荐

  1. 【Android 界面效果31】Android--侧滑菜单应用的实现

    侧滑菜单应用现在非常多,而且实现方式也多种多样.通过在网上的多方查找,我找到郭霖少侠的这篇文章:http://blog.csdn.net/guolin_blog/article/details/874 ...

  2. JQuery POST请求乱码...

    引言: 在JQuery的Ajax POST请求中,进行请求,其中的中文在后台,显示为乱码,该如何解决呢? 问题的引入: var regid = $('#oregion').combobox('getV ...

  3. 友盟分享各平台URL设置

    首先,想要进项友盟分享,需要到各平台去申请ID和KEY 比如想进行微信分享,就到微信开发者平台去创建应用,拿到对应的id和appScreat,然后进行设置: 参考资料

  4. 关于真机调试DDMS中的data文件夹打不开的解决方法

    关于真机调试DDMS中的data文件夹打不开的解决方法 今天在开发的时候需要导出程序中的数据库文件查看数据,数据库文件默认就在/data/data/应用包名/databases/数据库名 这个路径下, ...

  5. 【转】ConcurrentHashMap完全解析(JDK6/7、JDK8)

    转自http://my.oschina.net/hosee/blog/675884 并发编程实践中,ConcurrentHashMap是一个经常被使用的数据结构,相比于Hashtable以及Colle ...

  6. xcode报错 has been modified since the precompiled header was built

    删除/Users/username/Library/Developer/Xcode/DerivedData/ModuleCache 下的所有文件

  7. Slickflow.NET 开源工作流引擎基础介绍(二) -- 引擎组件和业务模块的交互

    集成流程引擎的必要性 业务过程的变化是在BPM系统中常见的现象,企业管理层需要不断优化组织架构,改造业务流程,不可避免地带来了业务流程的变化,企业信息系统就会随之面临重构的可能性.一种直接的方式是改造 ...

  8. 人情世故&潜规则

    大凡成功的牛人,无一例外都明白这一点.他们读懂了社会的本质和人际交往的潜规则,知道对方需要什么,知道对方脑子里在想什么.你几乎看不见他奔波劳碌,但是在不动声色中,他就已经实现人生目标.他们成功的密码是 ...

  9. PHP JS判断浏览器,微信浏览器

      微信内置浏览器的 User Agent 如何判断微信内置浏览器,首先需要获取微信内置浏览器的User Agent,经过在 iPhone 上微信的浏览器的检测,它的 User Agent 是: Mo ...

  10. Cocos2d-JS加速度计与加速度事件

    在很多移动设备的游戏使用到了加速度计,Cocos2d-JS引擎提供了访问加速度计传感器的能力.本节我们首先介绍一下加速度计传感器,然后再介绍如何在Cocos2d-JS中访问加速度计.加速度计加速度计是 ...