博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Topshelf 使用
阅读量:4601 次
发布时间:2019-06-09

本文共 1557 字,大约阅读时间需要 5 分钟。

前言

在写后台代码的过程中,经常会遇到要写一些单独的服务。以前呢,直接用的是 .NET 下的 “Windows 服务” 控件开发的。

这个传统的控件开发起来很不方面,使用也不友好。发现有用 Topshelf 的,这个第三方的框架,集成的很好,用起来也方便。

这里就说下我的使用过程。

使用

1、添加引用

在需要使用Topshelf的项目右键“引用”=》“管理NuGet程序包”

搜索“Topshelf”就可以,安装最新版。

2、代码中使用

这里直接上代码。

class Program    {        static void Main(string[] args)        {
Host host = HostFactory.New(x => {
          // 基本的配置 x.RunAsLocalSystem(); x.SetServiceName("Service"); x.SetDisplayName("Service"); x.SetDescription("服务"); x.StartAutomatically(); x.EnableShutdown();           // 注册服务 x.Service
(hostSettings => new Service()); // 设置服务失败后的操作,分别对应第一次、第二次、后续 x.EnableServiceRecovery(t => { t.RestartService(0); t.RestartService(0); t.RestartService(0); t.OnCrashOnly(); }); }); host.Run(); } }

这里要继承 Topshelf的“ServiceControl”,来开始服务和结束服务。

public class Service : ServiceControl    {
public bool Start(HostControl hostControl) { // 开始具体的业务逻辑 return true; } public bool Stop(HostControl hostControl) { // 结束 return true; } }

3、部署服务

部署、开始、卸载服务只需要一句命令行就可以:

安装:Service.exe install    启动:Service.exe start    卸载:Service.exe uninstall

这些命令是在当前文件夹下打开 CMD 执行的命令。如果不是当前文件夹,需要带上绝对路径。

转载于:https://www.cnblogs.com/zhurong/p/10311750.html

你可能感兴趣的文章
Python垃圾回收机制详解
查看>>
{面试题1: 赋值运算符函数}
查看>>
Node中没搞明白require和import,你会被坑的很惨
查看>>
Python 标识符
查看>>
Python mysql 创建连接
查看>>
企业化的性能测试简述---如何设计性能测试方案
查看>>
centos7 安装中文编码
查看>>
POJ - 3683 Priest John's Busiest Day
查看>>
正则表达式start(),end(),group()方法
查看>>
vuejs 学习旅程一
查看>>
javascript Date
查看>>
linux常用命令2
查看>>
狼图腾
查看>>
13、对象与类
查看>>
Sublime Text3 个人使用心得
查看>>
jquery 编程的最佳实践
查看>>
MeetMe
查看>>
IP报文格式及各字段意义
查看>>
(转载)rabbitmq与springboot的安装与集成
查看>>
C2. Power Transmission (Hard Edition)(线段相交)
查看>>