1 前置阅读

在阅读本文章之前,你可以先阅读:

  • Topshelf一个用于使用.NET构建Windows服务框架

2 使用

2.1 创建应用程序

首先,创建一个新的控制台应用程序并从nuget获取Topshelf和Microsoft.Extensions.Hosting软件包

Topshelf
Microsoft.Extensions.Hosting

当然我们也需要安装Serilog相关的日志框架。

Serilog.Extensions.Hosting
Serilog.Settings.Configuration
Serilog.Sinks.Console
Serilog.Sinks.File
Topshelf.Serilog

2.2 创建.NET泛型主机

然后,我们先建立CreateHostBuilder()方法,并加载了Serilog日志并依赖注入MyService和AppSettings,MyService类做为Topshelf所使用的主要逻辑程序,它会提供Start()和Stop()做为Topshelf执行或停止主要逻辑程序的动作。

class Program
{
static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
} public static IHostBuilder CreateHostBuilder(string[] args) =>
Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureServices((hostContext, services) =>
{
services.Configure<AppSettings>(hostContext.Configuration);
services.AddTransient<MyService>();
});
}

2.3 在Topshelf中注册服务

接着,在Topshelf中注册我们的服务类。跳转到Program.cs并添加:

class Program
{
static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
RunWindowsServiceWithHost(host);
} private static void RunWindowsServiceWithHost(IHost host)
{
var rc = HostFactory.Run(x =>
{
x.UseSerilog();
x.SetDisplayName("我的服务");
x.SetDescription("我的服务详细描述");
x.SetServiceName("MyService"); var myService = host.Services.GetRequiredService<MyService>();
x.Service<MyService>(s =>
{
s.ConstructUsing(() => myService);
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.StartAutomatically();
}); var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());
Environment.ExitCode = exitCode;
}
}

2.4 MyService类

接着,我们看看MyService类,主要演示了注入ILogger和AppSettings。

public class MyService
{
private readonly ILogger logger;
private readonly AppSettings settings; public MyService(IOptions<AppSettings> settings, ILogger<MyService> logger)
{
this.settings = settings.Value;
this.logger = logger;
}
public void Start()
{
logger.LogInformation($"Starting {this.settings.ServiceName}...");
} public void Stop()
{
logger.LogInformation($"Stopping {this.settings.ServiceName}...");
}
}

2.5 运行应用程序

最后,F5执行应用程序,如果一切顺利,你应该会看到类似以下内容的信息:

如何使用Topshelf与.NET泛型主机建立Windows服务的更多相关文章

  1. Topshelf一个用于使用.NET构建Windows服务框架

    1 Topshelf是什么? Topshelf是用于托管使用.NET框架编写的Windows服务的框架.服务的创建得到简化,从而使开发人员可以创建一个简单的控制台应用程序,可以使用Topshelf将其 ...

  2. 使用 Topshelf 结合 Quartz.NET 创建 Windows 服务

    Ø  前言 之前一篇文章已经介绍了,如何使用 Topshelf 创建 Windows 服务.当时提到还缺少一个任务调度框架,就是 Quartz.NET.而本文就展开对 Quartz.NET 的研究,以 ...

  3. 使用Topshelf管理Windows服务

    目的:以控制台方式开发Windows服务程序,调试部署方便. https://www.cnblogs.com/itjeff/p/8316244.html https://www.cnblogs.com ...

  4. 使用.NET Core创建Windows服务(二) - 使用Topshelf方式

    原文:Creating Windows Services In .NET Core – Part 2 – The "Topshelf" Way 作者:Dotnet Core Tut ...

  5. TPYBoard实例之利用WHID为隔离主机建立隐秘通道

    本文作者:xiaowuyi,来自FreeBuf.COM(MicroPythonQQ交流群:157816561,公众号:MicroPython玩家汇) 0引言 从2014年BADUSB出现以后,USB- ...

  6. 使用Topshelf 开发windows服务

    在业务系统中,我们为了调度一些自动执行的任务或从队列中消费一些消息,所以基本上都会涉及到后台服务的开发.如果用windows service开发,非常不爽的一件事就是:调试相对麻烦,而且你还需要了解 ...

  7. 使用Topshelf组件构建简单的Windows服务

    很多时候都在讨论是否需要了解一个组件或者一个语言的底层原理这个问题,其实我个人觉得,对于这个问题,每个人都有自己的看法,个人情况不同,选择的方式也就会不同了.我个人觉得无论学习什么,都应该尝试着去了解 ...

  8. .NET Core Generic Host Windows服务部署使用Topshelf

    此文源于前公司在迁移项目到.NET Core的过程中,希望使用Generic Host来管理定时任务程序时,没法部署到Windows服务的问题,而且官方也没给出解决方案,只能关注一下官方issue # ...

  9. 利用Topshelf把.NET Core Generic Host管理的应用程序部署为Windows服务

    背景 2019第一篇文章. 此文源于前公司在迁移项目到.NET Core的过程中,希望使用Generic Host来管理定时任务程序时,没法部署到Windows服务的问题,而且官方也没给出解决方案,只 ...

随机推荐

  1. js data type checker

    js data type checker js 数据类型检测 "use strict"; /** * * @author xgqfrms * @license MIT * @cop ...

  2. CSS will-change All In One

    CSS will-change All In One CSS animation effect live demo https://nextjs.org/conf/ https://nextjs.or ...

  3. HTTPS All In One

    HTTPS All In One HTTPS & web security HTTPS Hypertext Transfer Protocol Secure HTTPS is an exten ...

  4. bind & this & new & arrow function

    bind & this & new & arrow function this bind call apply new arrow function arrow functio ...

  5. 微信公众号 bug

    微信公众号 bug web bug refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  6. layui 时间插件laydate中动态设置改变min和max值

    <div class="layui-inline"> <label class="layui-form-label">申请时间</ ...

  7. Windows Server2012 r2 nginx反向代理图片服务器

    1.下载nginx  http://nginx.org/en/download.html,本人下载的1.18.0版本 2.下载 Windows Service Wrapper(winsw.exe) v ...

  8. hadoop环境搭建:完全分布式

    目录 1.硬件配置 2.软件版本 3.准备工作 3.1.建立虚拟机,网络设置为桥接模式 3.2.更改主机名 3.3.绑定主机名和IP,建立各主机间的联系 3.4.关闭防火墙 3.5.配置宿主机host ...

  9. SpringBoot整合Mybatis 使用generator自动生成实体类代码、Mapper代码、dao层代码

    1.新建一个SpringBoot项目,并引入Mybatis和mybatis-generator相关的依赖. <dependency> <groupId>org.springfr ...

  10. 用OkHttpGo和FastJson获取OneNET云平台数据(解析嵌套数组)

    JSON数据格式有两种,一种是 { } 大括号表示的JSON对象,一种是 [ ] 中括号表示的JSON数组.从OneNET获取到的数组是这样的,并用Json解析网址查看https://jsonform ...