.NET Core 创建Windows服务
.NET Core 创建Windows服务
写在前面
使用 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服务的更多相关文章
- 使用.NET Core创建Windows服务(二) - 使用Topshelf方式
原文:Creating Windows Services In .NET Core – Part 2 – The "Topshelf" Way 作者:Dotnet Core Tut ...
- 使用.NET Core创建Windows服务 - 使用.NET Core工作器方式
原文:Creating Windows Services In .NET Core – Part 3 – The ".NET Core Worker" Way 作者:Dotnet ...
- 使用.NET Core创建Windows服务(一) - 使用官方推荐方式
原文:使用.NET Core创建Windows服务(一) - 使用官方推荐方式 原文:Creating Windows Services In .NET Core – Part 1 – The &qu ...
- 使用.NET Core创建Windows服务详细步骤
目录 #创建步骤 1.使用Visual Studio创建 2.使用命令行创建 #项目结构说明 #将应用转换成Window服务 1.引入Microsoft.Extensions.Hosting.Wind ...
- 使用.NET Core中创建Windows服务(一) - 使用官方推荐方式
原文:Creating Windows Services In .NET Core – Part 1 – The "Microsoft" Way 作者:Dotnet Core Tu ...
- 使用.Net Core 2.2创建windows服务
使用.Net Core 2.2创建windows服务 我的环境 win 10 home Visual Studio 2019 v16.1.3 安装有.net core 2.2 创建项目 编辑项目文件 ...
- C#/.NET基于Topshelf创建Windows服务的守护程序作为服务启动的客户端桌面程序不显示UI界面的问题分析和解决方案
本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...
- 用C#创建Windows服务(Windows Services)
用C#创建Windows服务(Windows Services) 学习: 第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程中选取 Windows 服务(W ...
- 玩转Windows服务系列——创建Windows服务
创建Windows服务的项目 新建项目->C++语言->ATL->ATL项目->服务(EXE) 这样就创建了一个Windows服务项目. 生成的解决方案包含两个项目:Servi ...
随机推荐
- jQuery 的on()方法
jQuery 的on()方法 一.总结 一句话总结: 1.普通添加事件:$("a").on("click", function () {执行的代码}) 2.未创 ...
- VS Code文本编辑快捷操作(2)
1. 光标移动 移动光标最常用的就是方向键,但是方向键每次只能把光标移动一个位置,可以说是一种相对低效的方式.下面介绍针对单词.行.代码块.整个文档等多种光标移动方式. 1.1 ...
- Flutter移动电商实战 --(29)列表页_商品列表数据模型建立
简历数据模型 json生成dart类的网站: https://javiercbk.github.io/json_to_dart/ json数据 {"code":"0&qu ...
- ubuntu 上开发.netcore
ubuntu需要安装的软件: 1.sudo apt-get install openssh-server openssh-client 2.sudo apt-get git 3.安装vscode 4. ...
- Java同步数据结构之DelayQueue/DelayedWorkQueue
前言 前面介绍了优先级队列PriorityBlockingQueue,顺带也说了一下PriorityQueue,两者的实现方式是一模一样的,都是采用基于数组的平衡二叉堆实现,不论入队的顺序怎么样,ta ...
- Linux-文件系统的简单操作
文件系统的简单操作 磁盘与目录的容量:df.du df [option] [目录或文件名] 参数: -a:列出所有的文件系统,包括系统特有的/proc等系统 -k:以KB的容量显示各文件系统 -m:以 ...
- Spring Cloud(5):服务路由(Zuul)
Zuul简介 所有微服务之间的调用,都应该通过服务网关进行路由,服务网关充当服务与服务之间的中介.服务网关像交通警察一样指挥交通,将用户引导到目标微服务实例.服务网关还充当着应用程序内所有微服务调用的 ...
- Go单引号和双引号区别
首先做个测试,看下面那个选项是正确的: A. str:='abc'+'123'B. str:="abc"+"123"C. str:='123'+"ab ...
- Go语言实现bitmap算法
有关bitmap算法的介绍资料网上很多,这里不赘述,各种语言的实现也不少,但是Go语言版的bitmap不多,本文就来写一个Go版的bitmap实现. 首先创建一个 bitmap.go 文件,定义一个b ...
- 【计算机视觉】双目测距(六)--三维重建及UI显示
原文: http://blog.csdn.NET/chenyusiyuan/article/details/5970799 在获取到视差数据后,利用 OpenCV 的 reProjectImageTo ...