ASP.NET Core 6 应用程序无法找到 UseWindowsService

7
我的目标是以最简单的方式将ASP.NET Core 6应用程序作为Windows服务运行,我理解可以使用下面显示的代码。
我已经包含了这两行代码(虽然只有顶部的一行应该是必要的):
using Microsoft.AspNetCore.Hosting.WindowsServices;
using Microsoft.Extensions.Hosting.WindowsServices;

并安装了NuGet包:

<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="6.0.0" />

但是当使用 IWebHostBuilder 时,这段代码无法解决 .UseWindowsService() 问题:

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(Configuration)
            .ConfigureServices(ConfigureServices)
            .UseUrls(Configuration.GetBindHostUrl())
            .UseStartup<Startup>()
            .UseWindowsService();   // not found

我收到的错误信息是:
“IWebHostBuilder”没有“UseWindowsService”的定义,并且最佳扩展方法重载“WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder)”需要类型为“IHostBuilder”的接收器。
我在这里缺少什么呢?

只是为了明确一下:您安装了扩展的.nuget吗? - TheTanic
1
@TheTanic 是的 - 请查看问题的更新以获得更多的明确信息。 - James Harcourt
3个回答

7

你可以尝试使用更通用的IHostBuilder,而不是使用WebHost:

 var host = Host.CreateDefaultBuilder(args)
                .UseWindowsService()
                .UseSystemd()
                .ConfigureWebHostDefaults(webbuilder =>
                {
                    //Configure here your WebHost. For example Startup;
                    webbuilder.UseStartup<Startup>();
            });

我刚刚将一个 .net 5 项目(VS2019)迁移到了 .net 6(VS2022)。它设置了上述的主机构建器。在 VS2022 / .net 6 中,它抱怨错误 CS1061 'IHostBuilder' 不包含 'UseWindowsService' 的定义,但它仍然可以构建和运行(尽管我还没有尝试重新安装它作为服务)。感到困惑。 - Peter
9
不知道为什么它仍在构建,但要使用UseWindowsService()方法,您需要安装NuGet-Package - TheTanic

3

我刚从 .net 3 迁移到最新的 7.0 版本和 VS2019 到 2022,然后不得不从 NuGet 包管理器安装依赖项:

nuget aditional extensions

然后我就能在任何Windows或Linux版本上运行了。

作为参考,要创建一个Windows服务只需要一个批处理文件以管理员身份运行:

Install.cmd

chcp 1252>NUL
SET mypath=%~dp0

sc create "service.name" displayname= "display.name" binpath= %mypath:~0,-1%\app-name.exe start= auto
sc description "service.name" "service description"

NET START service.name

pause

Uninstall.cmd

NET STOP service.name
sc delete "service.name"
pause

0
Microsoft.Extensions.Hosting.WindowsServices是用于IHostBuilder的,这是新的3.0应用程序现在使用的。 Microsoft.AspNetCore.Hosting.WindowsServices是用于IWebHostBuilder的。
您必须引用此Nuget包(Microsoft.AspNetCore.Hosting.WindowsServices)并使用以下代码。
public static void Main(string[] args)
        {
           
            CreateWebHostBuilder(args).Build().RunAsService();

        }
        
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseContentRoot(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName))
                .UseStartup<Startup>()
                .UseKestrel((context, serverOptions) =>
                {
                    serverOptions.ListenAnyIP(1000);
                    serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(1.5);
                });

请参考下面的链接:https://github.com/dotnet/aspnetcore/issues/16804

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接