欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Topshelf创建Windows服务

程序员文章站 2022-07-11 07:58:26
该文章是系列文章 "基于.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官方文档地址:

创建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.