.NET Core 创建Windows服务

作者:高堂

原文地址:https://www.cnblogs.com/gaotang/p/10850564.html

写在前面

使用 TopShelf+Autofac+AutoMapper+Quartz+NLog 完成现有项目定时调度任务

1.相关NetGet包

  • 依赖注入 Alexinea.Autofac.Extensions.DependencyInjection
  • 对象映射 AutoMapper.Extensions.Microsoft.DependencyInjection
  • 调度 Autofac.Extras.Quartz
  • Topshelf注入 Topshelf.Autofac
  • Topshelf日志 Topshelf.NLog/Topshelf.Log4Net

2.添加Autofac自动映射服务

  • 引入 Alexinea.Autofac.Extensions.DependencyInjection

3.添加AutoMapper自动映射类

  • 引入 AutoMapper.Extensions.Microsoft.DependencyInjection
  • 添加测试类 User、UserDto
  • 添加服务类 IUserRepository、UserRepository、IUserService、UserService,获取配置文件中的User信息并转换为业务模型UserDto
  • 添加映射配置文件 MapperProfiles
  • 添加Autofac扩展 AutoMapperExtensions
public static class AutoMapperExtensions
{
public static ContainerBuilder ConfigureAutoMapper(this ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.AsClosedTypesOf(typeof(ITypeConverter<,>))
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof(AutoMapperExtensions).Assembly)
.AssignableTo<Profile>().As<Profile>(); builder.Register(c => {
var profiles = c.Resolve<IEnumerable<Profile>>();
var context = c.Resolve<IComponentContext>();
return new MapperConfiguration(x => {
foreach (var profile in profiles) x.AddProfile(profile);
x.ConstructServicesUsing(context.Resolve);
});
}).SingleInstance().AsSelf(); builder.Register(c => {
var context = c.Resolve<IComponentContext>();
var config = context.Resolve<MapperConfiguration>();
return config.CreateMapper();
}).As<IMapper>(); return builder;
}
}
  • 添加自定义服务扩展 ServicesExtensions
public static class ServicesExtensions
{
public static ContainerBuilder ConfigureSelf(this ContainerBuilder builder)
{
var services = new ServiceCollection();
// register appsettings.json
services.Configure<User>("UserConfig", Settings.Instance.Configuration.GetSection("User"));
builder.Populate(services);
// register services repositories
builder.RegisterAssemblyTypes(typeof(UserRepository).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof(UserService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces(); return builder;
}
}

4.添加Quartz调度任务

  • 引入 Autofac.Extras.Quartz
  • 添加调度任务类 MyJob1
  • 添加Autofac扩展 QuartzExtensions
public static ContainerBuilder ConfigureQuartz(this ContainerBuilder builder)
{
// 1) Register IScheduler
builder.RegisterModule(new QuartzAutofacFactoryModule());
// 2) Register jobs
builder.RegisterModule(new QuartzAutofacJobsModule(typeof(MyJob1).Assembly)); return builder;
}

5.添加日志

  • 引入 NLog.Extensions.Logging

6.创建TopShelf服务

  • 引入 Topshelf.NLog
  • 引入 Topshelf.Autofac

7.发布并添加Windows服务

  • 发布为独立文件

  • 添加安装(install.bat)和卸载文件(uninstall.bat)
cd /d %~dp0
Sample.Topshelf.exe install
pause
cd /d %~dp0
Sample.Topshelf.exe uninstall
pause

源码

参考资料

.NET Core 创建Windows服务的更多相关文章

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

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

  2. 使用.NET Core创建Windows服务 - 使用.NET Core工作器方式

    原文:Creating Windows Services In .NET Core – Part 3 – The ".NET Core Worker" Way 作者:Dotnet ...

  3. 使用.NET Core创建Windows服务(一) - 使用官方推荐方式

    原文:使用.NET Core创建Windows服务(一) - 使用官方推荐方式 原文:Creating Windows Services In .NET Core – Part 1 – The &qu ...

  4. 使用.NET Core创建Windows服务详细步骤

    目录 #创建步骤 1.使用Visual Studio创建 2.使用命令行创建 #项目结构说明 #将应用转换成Window服务 1.引入Microsoft.Extensions.Hosting.Wind ...

  5. 使用.NET Core中创建Windows服务(一) - 使用官方推荐方式

    原文:Creating Windows Services In .NET Core – Part 1 – The "Microsoft" Way 作者:Dotnet Core Tu ...

  6. 使用.Net Core 2.2创建windows服务

    使用.Net Core 2.2创建windows服务 我的环境 win 10 home Visual Studio 2019 v16.1.3 安装有.net core 2.2 创建项目 编辑项目文件 ...

  7. C#/.NET基于Topshelf创建Windows服务的守护程序作为服务启动的客户端桌面程序不显示UI界面的问题分析和解决方案

    本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...

  8. 用C#创建Windows服务(Windows Services)

    用C#创建Windows服务(Windows Services) 学习:  第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程中选取 Windows 服务(W ...

  9. 玩转Windows服务系列——创建Windows服务

    创建Windows服务的项目 新建项目->C++语言->ATL->ATL项目->服务(EXE) 这样就创建了一个Windows服务项目. 生成的解决方案包含两个项目:Servi ...

随机推荐

  1. Python 类中__init__()方法中的形参与如何修改类中属性的值

    一.__init__()方法 如果__init__()方法为 class Cat(): def __init__(self,num) : self.num=num Python中类的__init__( ...

  2. Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details.

    解决方案 https://blog.csdn.net/zt15732625878/article/details/86493096

  3. 记录学习Linux遇到的问题

    shl@shl-tx:~$ ifconfig Command 'ifconfig' not found, but can be installed with: sudo apt install net ...

  4. layer快速点击会触发多次回调

    场景还原 测试同学反馈点击了一次操作,为什么会有两条操作记录? 我:???? 排查思路 查看日志,看一下是不是发了两次请求,果不其然啊: 并发了,同一时间发送了两次请求,出现了脏写. 原因 系统的co ...

  5. Mapper抽象类参数

    Mapper< Object, Text, Text, IntWritable> Mapper< Text, Text, Text, Text> Mapper< Text ...

  6. 006-log-logback,slf4j+logback

    一.概述 Logback作为流行的log4j项目的继承者.它是由log4j的创始人Ceki Gulcu设计的.它是建立在上十年优质日志系统设计经验之上而产生的产品,即logback,它比所有现有的日志 ...

  7. C++ transform for_each

    #include<iostream>#include<vector>#include <list>#include <algorithm>#includ ...

  8. php文件断点上传

    前段时间做视频上传业务,通过网页上传视频到服务器. 视频大小 小则几十M,大则 1G+,以一般的HTTP请求发送数据的方式的话,会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长, ...

  9. linux双机热备份

    使用HeartBeat实现高可用HA的配置过程详解 一.写在前面 HA即(high available)高可用,又被叫做双机热备,用于关键性业务.简单理解就是,有2台机器 A 和 B,正常是 A 提供 ...

  10. Flutter 拖拽控件Draggable

    Flutter提供了强大的拖拽控件,可以灵活定制,并且非常简单.下面作一个拖拽的案例. Draggable Widget Draggable控件负责就是拖拽,父层使用了Draggable,它的子元素就 ...