简体   繁体   English

如何将 .NET Core 3.1 API web 应用程序作为 Z0F4137ED1502B5045426083AA258 服务托管?

[英]How to host a .NET Core 3.1 API web application as windows service?

I have an existing .NET Core 3.1 API web application which I'd like to host as a windows service.我有一个现有的 .NET Core 3.1 API web 应用程序,我想将它作为 Z0F4137ED1502B50445DZ 服务托管。 What is the best way to achieve this?实现这一目标的最佳方法是什么?

I've found several tutorials stating that adding the nuget package Microsoft.Extensions.Hosting.WindowsServices and using the UseWindowsService() function should be enough to host it as windows service. I've found several tutorials stating that adding the nuget package Microsoft.Extensions.Hosting.WindowsServices and using the UseWindowsService UseWindowsService() function should be enough to host it as windows service. This doesn't work for me.这对我不起作用。 I'll get Error 1053: The service did not respond to the start or control request in a timely fashion.我会收到错误 1053:服务没有及时响应启动或控制请求。

I've also read about worker services but I have no idea how this would work with an existing API project.我还阅读了有关工作人员服务的信息,但我不知道这将如何与现有的 API 项目一起使用。

My Program.cs looks like this:我的 Program.cs 看起来像这样:

 public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false)
                .Build();

            return Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseUrls(config.GetSection("Hosting:Url").Value);
                    webBuilder.UseStartup<Startup>();
                }).UseWindowsService();
        }
    }

Regarding the 1053 error for .net core windows service, might be of help to others:关于 .net 核心 windows 服务的 1053 错误,可能对其他人有帮助:

if the path to appsettings.json (or other external config files relative to base directory) is not correct, you get 1053如果appsettings.json (或其他相对于基本目录的外部配置文件)的路径不正确,则会得到 1053

three things, in my case, affected the base directory path:就我而言,三件事影响了基本目录路径:

  1. the publish option Produce single file was checked发布选项生成单个文件被选中
  2. started as a service from Services.exe (the same app, when opened as a console, had no problem)从 Services.exe 作为服务启动(同一个应用程序,当作为控制台打开时,没有问题)
  3. how you get the app base dir你如何获得应用程序基础目录

For the third point, I don't remember exactly, especially since I found these things through trial and error, but the following code worked for me:第三点,我记不太清了,特别是因为我通过反复试验找到了这些东西,但是下面的代码对我有用:

private static string GetBasePath()
{
    using var processModule = System.Diagnostics.Process.GetCurrentProcess().MainModule;
    return Path.GetDirectoryName(processModule?.FileName);
}

I'll try and test these things better when I have time and make it a little clearer当我有时间时,我会尝试更好地测试这些东西,让它更清楚一点

This is how I do it:我就是这样做的:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.WindowsServices;

namespace App.Name.API
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().RunAsService();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
    }
}

I use CreateWebHostBuilder instead of Create WebHostBuilder since im more used to it (and unsure of the difference between the two).我使用 CreateWebHostBuilder 而不是 Create WebHostBuilder 因为我更习惯它(并且不确定两者之间的区别)。

I think that by downloading the Microsoft.Extensions.Hosting.WindowsServices package, and then adding UseWindowsService() at the end of CreateHostBuilder in Program.cs, then publishing will be the easiest way.我认为通过下载Microsoft.Extensions.Hosting.WindowsServices package,然后在Program.cs的CreateHostBuilder末尾添加UseWindowsService UseWindowsService() ,那么发布将是最简单的方法。 (It should be your current plan.) (这应该是您当前的计划。)

I'll get Error 1053: The service did not respond to the start or control request in a timely fashion.我会收到错误 1053:服务没有及时响应启动或控制请求。

As for the error you are currently encountering, please refer to this document to resolve.至于您目前遇到的错误,请参考此文档解决。

You can publish your application completely by reading this article and turn it into a windows service.(By reading this article, I successfully fulfilled your needs.)您可以通过阅读本文将您的应用程序完全发布,并将其变成 windows 服务。(通过阅读本文,我成功满足了您的需求。)

There are two points to note.有两点需要注意。

When executing the cmd command, you need to right-click and select Run as Administrator , and then execute the sc command.执行 cmd 命令时,需要右键单击 select Run as Administrator身份运行,然后执行 sc 命令。

The executed command statement binPath and "=" can not have spaces, but the "=" and quotation marks must be separated, here is an example:执行的命令语句binPath和“=”不能有空格,但“=”和引号必须分开,这里是一个例子:

C:\WINDOWS\system32>sc create MyService binPath= "C:\Projects\WindowsServiceDemo\bin\Release\netcoreapp3.1\win-x64\WindowsServiceDemo.exe"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .net 核心 3.1 Web API 托管为 ZAEA23489CE3AA9B6406EBB28E0CDA 服务未显示页面 - .net core 3.1 Web API hosting as Windows Service not showing pages How to add .net core Web API project to an existing .NET core (3.1) Web Application project? - How to add .net core Web API project to an existing .NET core (3.1) Web Application project? 自主ASP.NET Web API和自主信号R一起使用Windows服务应用程序 - Self-host ASP.NET Web API and Self-host SignalR together windows service application 将服务主机(通过 TCP,非 Web)从 .NET Framework 4.8 移植到 .NET Core 3.1 - Porting service host (over TCP, non Web) from .NET Framework 4.8 to .NET Core 3.1 ASP.NET Core 3.1 Web API: can it be self-hosted as Windows service with https and use some certificate like IIS? - ASP.NET Core 3.1 Web API : can it be self-hosted as Windows service with https and use some certificate like IIS? 如何在 .NET Core 3.1 中调用 WCF web 服务? - How do I call a WCF web service in .NET Core 3.1? 如何在 .NET Core 3.1 应用程序中添加 WCF 服务参考? - How to add a WCF service reference in a .NET Core 3.1 application? .NET Core 3.1 控制台应用作为 Windows 服务 - .NET Core 3.1 Console App as a Windows Service 将 Autofac 与 ASP.Net Core 3.1 通用主机“Worker Service”应用程序一起使用 - Using Autofac with ASP.Net Core 3.1 generic host "Worker Service" application 如何在 .Net Core 3.1 Web API 控制器中获取声明? - How to get claims in .Net Core 3.1 Web API Controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM