Kestrel服务器
Posted harrychinese
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kestrel服务器相关的知识,希望对你有一定的参考价值。
Kestrel 服务器是什么
Kestrel 这个词的意思是红隼(小猛禽). 之前的 ASP.NET 应用深度绑定IIS服务, 跨平台和部署都是问题, 现在的 ASP.NET core 应用默认使用了 Kestrel web服务器, 有点类似于SpringBoot 默认内嵌了 tomcat. ASP.net core 还可以使用 Http.sys web服务器(仅限于Windows平台).
Program.cs文件中启用 Kestrel:
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseKestrel(options =>
//待配置
);
Kestrel 的特性
- 安全性较好, 支持https, 在MVC项目中我们通常调用
app.UseHttpsRedirection()
即可将 http请求重定向到 https 端口 - 性能很好, 早期的 Kestrel 是基于流行的libuv 异步I/O库
- 运行方便, 一行代码即可启动我们的应用.
dotnet MyApp.dll donet MyApp.dll --urls "http://localhost:8000;http://localhost:8001"
- 部署和配置方便
Kestrel 默认监听5000和5001端口, 我们可以在 appsettings.json 中修改端口, 也可在命令行中加--urls 指定.
"Kestrel":
"Endpoints":
"Http":
"Url": "http://localhost:5000"
,
"Https":
"Url": "https://localhost:5001"
关于 https
- UseHttpsRedirection 使用 HTTP(但重定向到 HTTPS)对终结点进行的请求失败,并返回 ERR_INVALID_REDIRECT on the CORS preflight request。
- Web API 项目推荐禁用 http 请求, 而不是通过 UseHttpsRedirection 进行https重定向
- 如果不指定证书,也可以使用 https,不过这使用的是默认的配置,只能用在 localhost 中。
参考
<https: www.cnblogs.com="" jackyfei="" p="" 16416868.html="">
<https: www.cnblogs.com="" jackyfei="" p="" 16586097.html="">
<https: learn.microsoft.com="" zh-cn="" aspnet="" core="" fundamentals="" servers="" kestrel="" endpoints?view="aspnetcore-6.0">
<https: www.tektutorialshub.com="" asp-net-core="" asp-net-core-kestrel-web-server="">
<https: geeksarray.com="" blog="" aspnet-core-application-and-kestrel-web-server-settings=""></https:></https:></https:></https:></https:>
如何检测 Kestrel 中的连接关闭
【中文标题】如何检测 Kestrel 中的连接关闭【英文标题】:How to detect a connection closure in Kestrel 【发布时间】:2021-12-02 05:13:29 【问题描述】:再一次,Microsoft 文档让我很不满意。我正在尝试找到正确的 API,我可以在其中配置回调以在客户端关闭其连接时进行捕获。
当我启动 gRPC 服务器时,我会在控制台中看到所有 Kestrel 配置和启动日志消息。当我启动 gRPC 客户端时,我可以在服务器的控制台日志中看到表明已建立连接的消息,如下所示:
dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[39]
Connection id "0HMCEE5LHGSKR" accepted.
dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[1]
Connection id "0HMCEE5LHGSKR" started.
当我通过单击关闭窗口按钮 (X) 关闭客户端时,我看到以下内容:
dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[19]
Connection id "0HMCEE5LHGSKR" reset.
dbug: Microsoft.AspNetCore.Server.Kestrel.Http2[48]
Connection id "0HMCEE5LHGSKR" is closed. The last processed stream ID was 1.
dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[7]
Connection id "0HMCEE5LHGSKR" sending FIN because: "The client closed the connection."
dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[2]
Connection id "0HMCEE5LHGSKR" stopped.
使用 ListenOptions.UseConnectionLogging(ListenOptions) 扩展方法的选项没有提供我可以找到的回调选项。显然,在默认中间件中,正在捕获事件,但我找不到该选项的路径。对 Microsoft.AspNetCore.Server.Kestrel 命名空间的检查表明(我找不到)如何获取 Microsoft.AspNetCore.Server.Kestrel.Connections 或 Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets 数据。
我正在使用 Visual Studio 2022、.NET 6、C# 10 和 gRPC。这是我当前的 Kestrel 配置:
// Configure Kestrel, the .NET Core web server.
var hostBuilder = webHostBuilder.ConfigureKestrel (kestrelServerOptions =>
kestrelServerOptions.ConfigureHttpsDefaults (httpsConnectionAdapterOptions => httpsConnectionAdapterOptions.SslProtocols = SslProtocols.Tls12);
// Read in the X.509 certificate file.
var certPath = Path.Combine (builder.Environment.ContentRootPath, "Certs", $"environment.pfx");
kestrelServerOptions.ConfigureEndpointDefaults (listenOptions =>
_ = listenOptions.UseHttps (certPath, password);
logger.Debug ($"Using certPath as the cert file.");
logger.Debug ("Configuring host to use HTTP/2 protocol.");
listenOptions.Protocols = HttpProtocols.Http2;
);
logger.Debug ("Reading config values for the server name and port.");
// Get the host name and port number to bind the service to.
var port = builder.Configuration.GetValue<int> ("AppSettings:OperationsServerPort");
var address = IPAddress.Parse ("0.0.0.0");
if (address != null)
logger.Debug ($"Host will listen at https://address:port");
kestrelServerOptions.Listen (address, port);
else
logger.Error ("DNS address for service host cannot be determined! Exiting...");
Environment.Exit (-1);
);
任何线索、指导、示例将不胜感激!
【问题讨论】:
【参考方案1】:好吧,我可能在理解所有这些 ASP.NET Core 配置方面迟到了,但是捕获来来往往的连接太简单了......只需将中间件委托添加到侦听器......
var ipEndpoint = new IPEndPoint (address, port);
kestrelServerOptions.Listen (ipEndpoint, action => action.Use (async (context, next) =>
Console.WriteLine ($"INTERNAL ---------------- New Connection: context.ConnectionId");
await next.Invoke ();
Console.WriteLine ($"INTERNAL ---------------- Connection terminated: context.ConnectionId");
));
这个 sn-p 通过添加中间件委托修改了我上面的原始帖子。可以在这里找到使我摆脱僵局的必读内容:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-5.0
我希望这对某人有帮助!
【讨论】:
以上是关于Kestrel服务器的主要内容,如果未能解决你的问题,请参考以下文章
ASP.NET Core Web服务器 Kestrel和Http.sys 特性详解