进入Controller前如何在.net中调试挂起的请求

时间:2019-01-24 13:25:41

标签: c# .net .net-core

如何调试对Web Api的HTTP请求,这些请求在不进入Controller的情况下仍保持待处理状态?

这是我的要求

  

卷曲“ http://localhost:50086/foo/bar” -X选项-H“访问控制请求方法:POST” -H“来源:http://localhost:3333” -H“用户代理:Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 70.0.3538.102 Safari / 537.36 OPR / 57.0.3098.116“ -H”访问控制请求报头:内容类型,x身份验证令牌“ --compressed

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

我正在使用.net Core 2.2

2 个答案:

答案 0 :(得分:3)

您可以为此使用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>();

}

答案 1 :(得分:0)

中间件已被其他用户建议。

您的另一个选择是使用ActionFilters。它们将在对控制器执行操作之前立即执行,并允许在控制器级别对请求进行精细控制。

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