如何检测Kestrel中的连接关闭



微软的文档再一次让我失望。我试图找到正确的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。这是我目前的红隼配置:

// 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);
}
});

任何线索,指导,例子将不胜感激!

好吧,我对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}");
}));

这个片段通过添加中间件委托修改了我上面的原始帖子。让我走出困境的必读书可以在这里找到:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-5.0

我希望这对某人有所帮助!!

相关内容

  • 没有找到相关文章

最新更新