该文章是系列文章 基于.NetCore和ABP框架如何让Windows服务执行Quartz定时做业 的其中一篇。git
能够访问ABP模板 建立项目 Demo.MyJob,ABP的模板是建立Web项目,而Topshelf所需的是Console项目。github
Topshelf开源地址:https://github.com/Topshelf/Topshelf
Topshef是一个简单的托管框架,经过Topshelf能够使用.Net简便的建立Windows服务。你须要先建立一个Console程序,而后使用Topshelf建立服务。调试控制台应用程序,确认服务可用性以后,安装便可。
Topshelf官方文档地址:https://topshelf.readthedocs.io/en/latest/bootstrap
新建Console程序Demo.MyJob,nuget添加Topshelfapp
由于要接入ABP,因此第一步要实现本身的AbpModule。框架
using System.Reflection; using Abp.Modules; namespace Demo.MyJob { [DependsOn(typeof(MyJobApplicationModule))] public class MyJobAbpModule : AbpModule { public override void Initialize() { IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly()); } } }
而后在Main函数入口处初始化咱们的ABP Job系统。async
using Abp; namespace Demo.MyJob { class Program { static void Main(string[] args) { using (var bootstrapper = AbpBootstrapper.Create<MyJobAbpModule>()) { bootstrapper.Initialize(); } } } }
定义操做ide
using System.Threading.Tasks; namespace Demo.MyJob { public class MyJobService { public async Task StartAsync() { //操做逻辑 } public async Task StopAsync() { //操做逻辑 } public async Task ContinueAsync() { //操做逻辑 } public async Task PauseAsync() { //操做逻辑 } } }
using System.Reflection; using Abp.Modules; using Topshelf; namespace Demo.MyJob { [DependsOn(typeof(MyJobApplicationModule))] public class MyJobAbpModule : AbpModule { public override void Initialize() { IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly()); } public override void PostInitialize() { HostFactory.Run(configure => { //定义服务描述 configure.SetDescription("Demo.MyJob Service"); configure.SetDisplayName("Demo.MyJob"); configure.SetServiceName("Demo.MyJob"); configure.RunAsLocalSystem(); //定义操做 configure.Service<MyJobService>(service => { service.ConstructUsing(_ => new MyJobService()); service.WhenStarted(async _ => await _.StartAsync()); service.WhenStopped(async _ => await _.StopAsync()); service.WhenContinued(async _ => await _.ContinueAsync()); service.WhenPaused(async _ => await _.PauseAsync()); }); }); } } }
Configuration Result: [Success] Name Demo.MyJob [Success] Description Demo.MyJob Service [Success] ServiceName Demo.MyJob Topshelf v4.2.0.194, .NET Framework v4.0.30319.42000 The Demo.MyJob service is now running, press Control+C to exit.
发布配置以下函数
<?xml version="1.0" encoding="utf-8"?> <!-- https://go.microsoft.com/fwlink/?LinkID=208121. --> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <PublishProtocol>FileSystem</PublishProtocol> <Configuration>Release</Configuration> <Platform>Any CPU</Platform> <TargetFramework>netcoreapp2.2</TargetFramework> <PublishDir>D:\Publish\Demo.MyJob</PublishDir> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <SelfContained>true</SelfContained> <_IsPortable>false</_IsPortable> </PropertyGroup> </Project>
以管理员身份打开cmd命令行终端,在发布目录下执行安装命令ui
C:\Windows\system32>D: D:\>cd D:\Publish\Demo.MyJob D:\Publish\Demo.MyJob>Demo.MyJob install Configuration Result: [Success] Name Demo.MyJob [Success] Description Demo.MyJob Service [Success] ServiceName Demo.MyJob Topshelf v4.2.0.194, .NET Framework v4.0.30319.42000 Running a transacted installation. Beginning the Install phase of the installation. Installing Demo.MyJob service Installing service Demo.MyJob... Service Demo.MyJob has been successfully installed. The Install phase completed successfully, and the Commit phase is beginning. The Commit phase completed successfully. The transacted install has completed.
卸载命令Demo.MyJob uninstall
,若是服务当前正在运行,该命令将先尝试中止服务,再将其移除。spa
D:\Publish\Demo.MyJob>Demo.MyJob uninstall Configuration Result: [Success] Name Demo.MyJob [Success] Description Demo.MyJob Service [Success] ServiceName Demo.MyJob Topshelf v4.2.0.194, .NET Framework v4.0.30319.42000 The uninstall is beginning. Uninstalling Demo.MyJob service Service Demo.MyJob is being removed from the system... Service Demo.MyJob was successfully removed from the system. Attempt to stop service Demo.MyJob. The uninstall has completed.