如何在进入控制器之前调试 .net 中的挂起请求



如何调试针对Web Api的HTTP请求,这些请求在不进入控制器的情况下保持挂起状态?

这是我的要求

curl "http://localhost:50086/foo/bar" -X 选项 -H "访问控制请求方法: POST" -H "

起源: http://localhost:3333" -H "用户代理: Mozilla/5.0 (Windows NT 10.0;赢64;x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 OPR/57.0.3098.116" -H "Access-Control-Request-Headers: content-type,x-auth-token" --compression

事件日志和调试控制台中没有任何内容。这些挂起的请求是随机发生的,并且在重新启动应用程序之前不会通过(在某些情况下,它们会进入控制器,但会在很长时间后)。

我正在使用.net Core 2.2

您可以使用Middleware来实现此目的。

public class RequestDiagnosticsMiddleware
{
    private readonly RequestDelegate _next;
    public RequestDiagnosticsMiddleware(RequestDelegate next)
    {
        _next = next;
    }
    public async Task InvokeAsync(HttpContext context)
    {
        // I don't yet called Controller/Action.
        //log the essential parts of the request here
        // Call the next delegate/middleware in the pipeline
        await _next(context);
    }
}

然后将中间件作为服务添加到启动配置中。

public void Configure(IApplicationBuilder app)
{
    app.UseMiddleware<RequestDiagnosticsMiddleware>();
}

另一个用户建议使用中间件。

您的另一个选择是使用操作筛选器。这些在控制器上的操作之前立即执行,并允许在控制器级别对请求进行细粒度控制。

请查看

以下链接以获取更详细的说明:https://code-maze.com/action-filters-aspnetcore/

最新更新