c# - 在 https 下使用 Kestrel 作为 Windows 服务运行 .NET Core Web API
问题描述
这个想法是,我想运行一个启用了 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 cancelToken)
在 Microsoft.AspNetCore .Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 应用程序,CancellationToken cancelToken)
在 Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancelToken) 在 Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancelToken) 在 Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) 在 Microsoft。 Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host) at DunaPrintServiceWP.Program.Main(String[] args) in d:\Visual Studio 2008\Projects\ OneCM.StoreCRM\DunaPrintServiceWP\Program.cs:第 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 }
}
}
}
解决方案
推荐阅读
- mysql - 使用函数'json_extract'时,指定Map映射问题
- javascript - Vuedraggable - 将项目拖到对象内的数组中
- python - pysnmp getSNMPDataResponse 函数返回无
- python - 使用标准 python -m unittest 运行测试
命令 - android - 为什么我的 playClick 和 stopClick 按钮从未使用过?
- c# - 在 C# 中将字符串转换为比较操作
- javascript - Reactjs 在保持正确纵横比的同时调整图像大小
- python - 在屏幕上居中 Qlabel
- flutter - 在 Flutter 中自动在应用屏幕加载时显示模态
- sql - REDSHIFT:根据另一列中的某个值更改列中的值