在 https 下使用 Kestrel 作为 Windows 服务运行 .NET Core Web API

问题描述 投票:0回答:1

我的想法是,我想运行一个启用了 Kestrel 并配置为公开 https 的 .NET Core Web API,该服务供内部使用,并且 js 应用程序应该在 https 下调用 localhost:someport。

我通过 SC cli 将 API 安装为 Windows 服务,安装得很好。一旦我启动服务,我就会收到以下错误:

无法配置 HTTPS 端点。未指定服务器证书,默认开发者证书找不到或已过期

但是当我从 VS 运行它时,它运行得很好并且顺利接受证书。

证书也安装在本地受信任的根 CA 中。

应用程序:DunaPrintServiceWP.exe
CoreCLR 版本:5.0.1121.47308
.NET版本:5.0.11

描述:由于未处理的异常,进程被终止。
异常信息:System.InvalidOperationException:无法配置 HTTPS 端点。未指定服务器证书,默认开发者证书找不到或已过期。 要生成开发人员证书,请运行“dotnet dev-certs https”。要信任证书(仅限 Windows 和 macOS),请运行“dotnet dev-certs https --trust”。

有关配置 HTTPS 的更多信息,请参阅 https://go.microsoft.com/fwlink/?linkid=848054

在 Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader.Reload()
在 Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader.Load()
在 Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken CancellationToken)
在 Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 应用程序,CancellationToken CancellationToken)
在 Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken CancellationToken) 在 Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken CancellationToken) 在 Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost 主机,CancellationToken 令牌) 在 Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost 主机,CancellationToken 令牌) 在 Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost 主机) 在 d:\Visual Studio 2008\Projects\OneCM.StoreCRM\DunaPrintServiceWP\Program.cs 中的 DunaPrintServiceWP.Program.Main(String[] args):第 19 行

launchSettings.Json

///trimmed for brevity

 "DunaPrintServiceWP": {
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "externalUrlConfiguration": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:9123;https://localhost:9124",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }

程序.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        
        .UseWindowsService(config =>
        {
            config.ServiceName = "DFPS_WP";
        })
        
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
            webBuilder.UseUrls();

            webBuilder.ConfigureKestrel(options =>
            {
                var port = 9124;
                var pfxFilePath = @"c:\certs\bella.pfx";
                // I've hard-coded it here just to make it easier to see what's going on.
                var pfxPassword = "Asd.Zxc1@#";

                options.Listen(IPAddress.Any, port, listenOptions =>
                {
                    // Enable support for HTTP1 and HTTP2 (required if you want to host gRPC endpoints)
                    listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
                    // Configure Kestrel to use a certificate from a local .PFX file for hosting HTTPS
                    listenOptions.UseHttps(pfxFilePath, pfxPassword);
                });
            });
        });

appSettings.json

"Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http1AndHttp2"
    },
    "Endpoints": {
      "HTTP": {
        "Url": "http://localhost:9123"
      },
      "HTTPS": {
        "Url": "https://localhost:9124",
        "ClientCertificateMode": "NoCertificate",
        "Protocols": "Http1AndHttp2",
        "SslProtocols": [ "Tls13", "Tls12", "Tls11", "Tls" ],
        "Certificate": { "AllowInvalid": true }
      }
    }
}
c# ssl .net-core windows-services kestrel
1个回答
0
投票

你解决这个问题了吗?遇到同样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.