【问题标题】:How to debug pending request in .net before entering Controller如何在进入控制器之前在.net中调试挂起的请求
【发布时间】:2019-06-18 06:04:06
【问题描述】:

如何调试向 Web Api 发送的 HTTP 请求,这些请求在未进入 Controller 的情况下仍处于 Pending 状态?

这是我的要求

curl "http://localhost:50086/foo/bar" -X OPTIONS -H "访问控制请求方法:POST" -H "来源:http://localhost:3333" -H "用户代理:Mozilla/5.0 (Windows NT 10.0; Win64 ; 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" --compressed

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

我正在使用 .net Core 2.2

【问题讨论】:

  • 人们投票以too broad 关闭您的帖子,您需要添加到您的帖子并使其更具体,否则它可能会在您得到答案之前关闭。将您用于发出这些 API 请求的代码添加到您的帖子中,以及它们随机发生的含义。
  • 事件日志和调试控制台不记录 HTTP 请求。您的 Web 服务器的日志就是这样做的。你在使用 IIS 吗?你检查过 IIS 的日志吗?
  • @Mirhat 当我说要发布您的代码以发出 API 请求时,我的意思是发布您的 C# code 以发出请求。
  • 看看 Telerik Fiddler 看看它是否有帮助。
  • @RyanWilson 我看不出这有什么帮助。在这种情况发生之前,请求工作正常。

标签: c# .net .net-core


【解决方案1】:

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

}

【讨论】:

    【解决方案2】:

    中间件已被其他用户推荐。

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

    更详细的解释请看以下链接:https://code-maze.com/action-filters-aspnetcore/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2013-08-16
      相关资源
      最近更新 更多