Loading

使用Topshelf创建Windows服务

该文章是系列文章 基于.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

实现AbpModule

因为要接入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();
            }
        }
    }
}

定义MyJobService

定义操作

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.
posted @ 2019-05-07 10:57  repeatedly  阅读(1146)  评论(2编辑  收藏  举报