使用Topshelf组件 一步一步创建 Windows 服务
我们先来介绍一下使用它的好处,以下论述参考自其他大神。
topshelf是创建windows服务的一种方式,相比原生实现ServiceBase、Install.Installer更为简单方便, 我们只需要几行代码即可实现windows服务的开发。
topshelf本身支持windows及linux下mono上部署安装,同样也是开源的。
topshelf相对原生来说,调试起来比较方便,可以在开发时以控制台的形式直接f5调试,发布时用命令以服务的形式部署。
还一个比较有用的特性是支持多实例的部署,这样可以在一台机器上部署多个相对的服务。类似的工具有instsrv和srvany。
多实例有一个好处就是容灾,当一个服务部署多份时,这样其中任何一个服务实例挂了,剩余的可以继续执行。
多实例可以是主备的方式,主挂了备服务才会执行。也可以以负载均衡的方式实现,多实例抢占进程锁或分布式锁,谁拿到谁执行。
先写出具体步骤:
// 新建控制台应用程序
// 使用Nuget安装Topshelf,选择能用的最新版本
// 使用Nuget安装NLog和NLog.config,选择能用的最新版本,用于打印日志 Nlog需要配置文件,详见NLog.config
// 初始化配置文件,创建AppConfigHelper类,继承 ConfigurationSection (需要引用System.Configuration程序集)
// 完善App.Config配置文件,读取App.Config配置文件,具体查看AppConfigHelper类
// 创建一个注册服务类TopshelfRegistService,初始化Topshelf注册
// 我们的目标很简单,就是让服务打印一个日志文件
// 编译并生成项目,进入 bin\Debug 目录下,找到xxx.exe 执行 install 命令,Windows 服务就诞生了
// 注意:如果出现需要以管理员身份启动的提示,重新以管理员身份启动 cmd
//接下来直接上代码与截图
卸载服务:
当我们启动服务的时候,成功打印出了日志,表示一切成功
程序结构很简单,如下图所示:
接下来,我们直接上实现代码,我会按照步骤依次给出:
1,Program主程序代码
namespace ProcessPrintLogService
{
class Program
{
public static readonly Logger log = LogManager.GetCurrentClassLogger();
private static readonly AppConfigHelper config = AppConfigHelper.Initity();
static void Main(string[] args)
{
TopshelfRegistService.Regist(config, true);
}
}
}
2.AppConfigHelper类,用于读取配置文件,使用配置文件的方式可以使你后期将该服务应用于多个应用程序
namespace ProcessPrintLogService
{
public class AppConfigHelper : ConfigurationSection
{
private static AppConfigHelper _AppConfig = null;
private static readonly object LockThis = new object(); /// <summary>
/// 获取当前配置 获取section节点的内容
/// 使用单例模式
/// </summary>
/// <returns></returns>
public static AppConfigHelper Initity()
{
if (_AppConfig == null)
{
lock (LockThis)
{
if (_AppConfig == null)
{
//获取app.config文件中的section配置节点
_AppConfig = (AppConfigHelper)ConfigurationManager.GetSection("AppConfigHelper");
}
}
}
return _AppConfig;
} //创建一个AppConfigHelper节点
//属性分别为:ServiceName、Desc 等....
//这里介绍一下属性标签:ConfigurationProperty 它可以在配置文件中根据属性名获取Value值
//可以参考文章https://www.cnblogs.com/liunlls/p/configuration.html /// <summary>
/// 服务名称
/// </summary>
[ConfigurationProperty("ServiceName", IsRequired = true)]
public string ServiceName
{
get { return base["ServiceName"].ToString(); }
internal set { base["ServiceName"] = value; }
} /// <summary>
/// 描述
/// </summary>
[ConfigurationProperty("Desc", IsRequired = true)]
public string Description
{
get { return base["Desc"].ToString(); }
internal set { base["Desc"] = value; }
} }
}
3.Topshelf组件注册服务
namespace ProcessPrintLogService
{
/// <summary>
/// Topshelf组件注册服务
/// </summary>
internal class TopshelfRegistService
{
/// <summary>
/// 注册入口
/// </summary>
/// <param name="config">配置文件</param>
/// <param name="isreg">是否注册</param>
public static void Regist(AppConfigHelper config, bool isreg = false)
{
//这里也可以使用HostFactory.Run()代替HostFactory.New()
var host = HostFactory.New(x =>
{
x.Service<QuartzHost>(s =>
{
//通过 new QuartzHost() 构建一个服务实例
s.ConstructUsing(name => new QuartzHost());
//当服务启动后执行什么
s.WhenStarted(tc => tc.Start());
//当服务停止后执行什么
s.WhenStopped(tc => tc.Stop());
//当服务暂停后执行什么
s.WhenPaused(w => w.Stop());
//当服务继续后执行什么
s.WhenContinued(w => w.Start());
});
if (!isreg) return; //默认不注册 //服务用本地系统账号来运行
x.RunAsLocalSystem();
//服务的描述信息
x.SetDescription(config.Description);
//服务的显示名称
x.SetDisplayName(config.ServiceName);
//服务的名称(最好不要包含空格或者有空格属性的字符)Windows 服务名称不能重复。
x.SetServiceName(config.ServiceName);
});
host.Run(); //启动服务 如果使用HostFactory.Run()则不需要该方法
}
} /// <summary>
/// 自定义服务
/// </summary>
internal class QuartzHost
{
public readonly Logger log = LogManager.GetLogger("QuartzHost"); public QuartzHost()
{
var service = AppConfigHelper.Initity();
} //服务开始
public void Start()
{
try
{
Task.Run(() =>
{
log.Info($"服务开始成功!");
});
}
catch (Exception ex)
{
Task.Run(() =>
{
log.Fatal(ex, $"服务开始失败!错误信息:{0}", ex);
});
throw;
}
} //服务停止
public void Stop()
{
Task.Run(() =>
{
log.Trace("服务结束工作");
});
}
} }
4.App.config配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration> <!--该节点一定要放在最上边-->
<configSections>
<section name="AppConfigHelper" type="ProcessPrintLogService.AppConfigHelper,ProcessPrintLogService"/>
</configSections> <!--TopSelf服务配置文件 -->
<AppConfigHelper
ServiceName="Process_PrintLogService"
Desc="日志打印服务"
/> <!--数据库连接字符串 -->
<connectionStrings>
<add name="ConnectionString" connectionString=""/>
</connectionStrings> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
5.Nlog.config日志配置文件
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<!--type="File|Console" 属性是设置日志输出目标是"File"(文件)或者"Console"(控制台)-->
<!--fileName="${basedir}/logs/${shortdate}/${level}/${callsite}.log" 设置日记记录文件的路径和名称-->
<!--layout="${longdate} ${level} ${callsite}:${message}" 设置日志输出格式-->
<target name="t1"
type="File"
fileName="${basedir}/logs/${shortdate}/${level} ${callsite}.log"
layout="${longdate} ${level} ${callsite}:${message}"
archiveAboveSize=""
archiveNumbering="Rolling"
concurrentWrites="false"
keepFileOpen="true"
maxArchiveFiles =""
/> <!--输出至控制台-->
<target name="t2" type="Console" layout="${longdate} ${level} ${callsite}:${message}" />
</targets> <rules>
<!--如果填*,则表示所有的Logger都运用这个规则,将所有级别的日志信息都写入到“t1”和“t2”这两个目标里-->
<logger name="*" writeTo="t1,t2"/>
</rules>
</nlog>
以上就是此次示例的全部代码,到此你也许会有一个问题,就是我想定时执行我的任务?比如每天几点执行,或者每几分钟执行一次等等,那我们该怎么做呢?
答案是使用:Quartz.net ,接下来我将会使用 Quartz.net 实现上述的定时任务。
参考文献:
https://www.jianshu.com/p/f2365e7b439c
http://www.80iter.com/blog/1451523192435464/
https://www.itsvse.com/thread-7503-1-1.html?tdsourcetag=s_pctim_aiomsg
https://www.cnblogs.com/yanglang/p/7199913.html
使用Topshelf组件 一步一步创建 Windows 服务的更多相关文章
- 使用 Topshelf 组件一步一步创建 Windows 服务 (2) 使用Quartz.net 调度
上一篇说了如何使用 Topshelf 组件快速创建Windows服务,接下来介绍如何使用 Quartz.net 关于Quartz.net的好处,网上搜索都是一大把一大把的,我就不再多介绍. 先介绍需要 ...
- 使用Topshelf 5步创建Windows 服务 z
使用Topshelf创建Windows 服务简要的介绍了创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with T ...
- 使用Topshelf 5步创建Windows 服务
使用Topshelf创建Windows 服务简要的介绍了创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with T ...
- C# 使用Vici WinService组件来创建Windows服务
Vici WinService 是 Windows平台下使用C#开发的轻量级用于创建,删除服务的类库,您只需简单的几行代码即可实现多线程异步服务的创建,删除,运行 废话不多说,直接上代码 /***** ...
- 使用Topshelf创建Windows服务
概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...
- [Solution] Microsoft Windows 服务(2) 使用Topshelf创建Windows服务
除了通过.net提供的windows服务模板外,Topshelf是创建Windows服务的另一种方法. 官网教程:http://docs.topshelf-project.com/en/latest/ ...
- Topshelf创建Windows服务
使用Topshelf创建Windows服务 概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps ...
- 使用 Topshelf 结合 Quartz.NET 创建 Windows 服务
Ø 前言 之前一篇文章已经介绍了,如何使用 Topshelf 创建 Windows 服务.当时提到还缺少一个任务调度框架,就是 Quartz.NET.而本文就展开对 Quartz.NET 的研究,以 ...
- 使用 Topshelf 创建 Windows 服务
Ø 前言 C# 创建 Windows 服务的方式有很多种,Topshelf 就是其中一种方式,而且使用起来比较简单.下面使用 Visual Studio Ultimate 2013 演示一下具体的使 ...
随机推荐
- CVE-2019-0708(非蓝屏poc)远程桌面代码执行漏洞复现
玩了几天 刚回成都 玩电脑复现一下~ 内核漏洞原理暂时 没看懂 别问 ,问就是不懂 0x01 复现环境和Exp准备 漏洞影响范围 Windows 7 Windows Server 2008 R2 W ...
- PMBOK(第六版) PMP笔记——《十》第十章(项目沟通管理)
PMBOK(第六版) PMP笔记——<十>第十章(项目沟通管理) 第十章 项目沟通管理: PM 大多数时间都用在与干系人的沟通上. 第十章有三个过程: 规划沟通管理:根据干系人的需求,制定 ...
- IDE安装完成无法打开,报错Fail load JVM DLL 问题与解决方案
安装完成pycharm 在打开pycharm的时候出现报错 Fail load JVM DLL xxxx xxx. 解决方案 安装Microsoft Visual C++ 2010 Redistrib ...
- 百万年薪python之路 -- 模块二
1. 序列化模块 什么是序列化呢? 序列化的本质就是将一种数据结构(如字典.列表)等转换成一个特殊的序列(字符串或者bytes)的过程就叫做序列化. 为什么要有序列化模块? 如果你写入文件中的字符串是 ...
- MOOC C++笔记(七)输入输出流
输入输出流 与输入输出流操作相关的类 istream:是用于输入的流类,cin就是该类的对象. ostream:是用于输出的流类,cout就是该类的对象. ifstream:是用于从文件读取数据的类. ...
- redis系列之------对象
前言 Redis 并没有直接使用数据结构来实现键值对数据库, 而是基于这些数据结构创建了一个对象系统, 这个系统包含字符串对象.列表对象.哈希对象.集合对象和有序集合对象这五种类型的对象, 每种对象都 ...
- UIAlert
转自:https://blog.csdn.net/deng0zhaotai/article/details/53887508 通过uialertcontroller实现三种简易弹框 (一)警告类 - ...
- 2018.8.8 python 初识函数
主要内容: 1.函数定义 2.函数名,函数体以及函数的调用 3.函数的返回值 4.函数的参数 一.函数的定义 函数:对代码块和功能的封装和定义. 二.函数名.函数体及函数的调用 我们使用def关键字来 ...
- MySQL学习之路(1):SQL脚本语言
使用MySQL数据库,首先安装MySQL数据库,本文所有SQL脚本在MySQL上测试和执行. 安装Mysql服务器:安装Mysql workbench客户端,可以以图形化界面管理mysql:安装php ...
- javadoc的使用
在进行项目开发过程中,项目接口文档是很重要的一块内容,在java项目中我们可以用swagger,asciidoc,javadoc等方式来生产文档,而其中最基本的文档生成方式就是javadoc,它一般用 ...