当前位置: 移动技术网 > IT编程>开发语言>.net > windows 服务中托管asp.net core

windows 服务中托管asp.net core

2018年08月26日  | 移动技术网IT编程  | 我要评论

狗屎花,目连大叫,兰陵缭乱第二部

在windows 服务中托管asp.net core

sdk 2.1.300


1、添加运行标识符
xml <propertygroup> <targetframework>netcoreapp2.1</targetframework> <runtimeidentifier>win7-x64</runtimeidentifier> </propertygroup>
2、添加包引用 dotnet add package microsoft.aspnetcore.hosting.windowsservices -v 2.1.0 -s



3、确认导入是否成功
xml <itemgroup> <packagereference include="microsoft.aspnetcore.app" /> <packagereference include="microsoft.aspnetcore.hosting.windowsservices" version="2.1.0" /> </itemgroup>
4、修改program main 函数

 public static void main(string[] args)
{
     createwebhostbuilder(args).build().runasservice();
 }

public static iwebhostbuilder createwebhostbuilder(string[] args)
 {
    var pathtoexe = process.getcurrentprocess().mainmodule.filename;
    var pathtocontentroot = path.getdirectoryname(pathtoexe);
    return webhost.createdefaultbuilder(args)
            .usekestrel()
            .useurls("http://*:5001", "http://*:5002")
            .configureappconfiguration((context, config) =>
            {
                // configure the app here.
            })
            .usecontentroot(pathtocontentroot)
            .usestartup<startup>();
    }

5、发布运行

dotnet publish -c release -o "f:\winservices\mvcapp21"

6、使用sc.exe工具创建服务 此处使用系统原始dos命令

发布根目录下:sc create mvcapp21 binpath= "f:\winservices\mvcapp21\mvcapp21.exe"

确保 binpath= 参数与其值之间存在空格

启动服务 sc start myservice
查看服务状态 sc query myservice
停止服务 sc stop myservice
删除服务 sc delete myservice

7、可能遇到的问题

8、.net core 部署服务其他方案

使用nssm把.net core部署至windows 服务

9、回顾.net 部署服务

使用topshelf创建windows 服务

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网