該文章是系列文章 "基於.NetCore和ABP框架如何讓Windows服務執行Quartz定時作業" 的其中一篇。 可以訪問 "ABP模板" 創建項目 Demo.MyJob,ABP的模板是創建Web項目,而Topshelf所需的是Console項目。 Topshelf是什麼 Topshelf開源地 ...
該文章是系列文章 基於.NetCore和ABP框架如何讓Windows服務執行Quartz定時作業 的其中一篇。
可以訪問ABP模板 創建項目 Demo.MyJob,ABP的模板是創建Web項目,而Topshelf所需的是Console項目。
Topshelf是什麼
Topshelf開源地址:https://github.com/Topshelf/Topshelf
Topshef是一個簡單的托管框架,通過Topshelf可以使用.Net簡便的創建Windows服務。你需要先創建一個Console程式,然後使用Topshelf創建服務。調試控制台應用程式,確認服務可用性之後,安裝即可。
Topshelf官方文檔地址:https://topshelf.readthedocs.io/en/latest/
創建Console,集成Topshelf
新建Console程式Demo.MyJob,nuget添加Topshelf
因為要接入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系統。
using Abp;
namespace Demo.MyJob
{
class Program
{
static void Main(string[] args)
{
using (var bootstrapper = AbpBootstrapper.Create<MyJobAbpModule>())
{
bootstrapper.Initialize();
}
}
}
}
定義操作
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命令行終端,在發佈目錄下執行安裝命令
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
,如果服務當前正在運行,該命令將先嘗試停止服務,再將其移除。
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.