.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. Linux 时间同步 Chrony

    chrony 是网络时间协议(NTP)的通用实现. chrony 包含两个程序:chronyd 是一个可以在启动时启动的守护程序.chronyc 是一个命令行界面程序,用于监视 chronyd 的性能 ...

  2. redus - 队列

    redus 写如队列 <?php $redis = new \Redis(); $redis->connect('127.0.0.1',6379); $password = '123456 ...

  3. js获取本地ip

    function getUserIP(onNewIP) { // onNewIp - your listener function for new IPs //compatibility for fi ...

  4. dnspy使用技巧

    打开dnspy,调试–>附加到进程–>选择相应的进程ID–>附加(支持同时附加多个进程) 调试–>窗口–>模块–>搜索要调试的程序集–>双击(这一步很重要, ...

  5. ubuntu 命令行以及目录的显示改为英文

    在安装ubuntu的时候原本以为把ubuntu改为中文的好理解一些,最后发现命令行的报错一边中文一边英文确实难受,也不宜于学习,所以想把命令行以及目录的显示都改为英文的,我是这样先把命令行的该为英文的 ...

  6. ubuntu 切换默认python版本

    现在的python项目都是基于python3的了,再用ubuntu的时候默认的版本是py2的,所以想切换到py3上: 打开终端: sudo update-alternatives --install ...

  7. Unix/Linux下如何查看DNS服务器地址

    使用命令: cat /etc/resolv.conf 或者 less /etc/resolv.conf 即可. 详细请见:http://www.cyberciti.biz/faq/how-to-fin ...

  8. springboot之activemq安装与实践

    环境:腾讯云centos7 注意:activemq安装插件,可能会报错.本人是主机名的问题,所以修改了主机名. vim /etc/hosts vim /etc/hostname 修改这两个文件,并重启 ...

  9. Freemarker使用总结

    spring mvc 中 Freemarker 获取项目根目录,方便HTML页面配置各种路径 在SpringMVC框架中使用Freemarker试图时,要获取根路径的方式如下: <!-- Fre ...

  10. oracle数据库可视化工具

    1.TreeSoft基于web网页方式,管理维护oracle数据,功能包括:SQL在线执行,数据在线维护管理,数据导出,数据交换同步等. 支持MySQL,Oracle,DB2,PostgreSQL,S ...