【问题标题】:WebAPI Response never completes when returning HttpResponseMessage and Application Insights is configured返回 HttpResponseMessage 并配置 Application Insights 时,WebAPI 响应永远不会完成
【发布时间】:2015-03-26 03:08:11
【问题描述】:

我有一个 MVC5/WebAPI2 应用程序,自从我创建 Web 项目以来,它就启用了 Application Insights。

返回对象(例如字符串、模型对象)的 WebApi 方法按预期返回 - 序列化为 JSON 或 XML。

 public class TestController : ApiController
{
    [HttpGet]
    [AllowAnonymous]
    async public Task<HttpResponseMessage> ReadString(int id) {

        HttpResponseMessage response = Request.CreateResponse();

        string str;
        using (HttpClient client = new HttpClient()) {
            Uri uri = new Uri("http://someurl.com/resource.rss");
            str = await client.GetStringAsync(uri);
        }

        response.Content = new StringContent(str);
        response.Content.Headers.ContentLength = str.Length;

        return response;
    }
}

当我创建一个返回 HttpResponseMessage 的操作时,我注意到浏览器中有一个奇怪的行为(用 Chrome 和 IE 测试)。具体来说,我的内容已返回到浏览器,但在我点击停止按钮或停止 Web 服务器 (Visual Studio 2013) 之前,“忙碌”指示器从未停止旋转。

我已验证上述方法在没有 Application Insights 的 Web 应用中按预期工作。具体来说,一旦数据返回给浏览器,响应就结束了。当使用 Application Insights 将以下方法添加到新创建的应用程序时,会遇到前面描述的行为。

有什么想法吗?

【问题讨论】:

  • 这似乎是由 Application Insights 添加的检测代码触发的 ASP.NET 错误。我在我们的内部问题跟踪系统中创建了一个错误,一旦修复或有解决方法,我将在此处发布更新。

标签: c# asp.net-web-api asp.net-web-api2 azure-application-insights


【解决方案1】:

这是由 ASP.NET 请求管道中引发的 NullReferenceException 引起的。 Microsoft.ApplictionInsights.Web 包正在处理触发问题的HttpApplication.PreSendRequestHeaders 事件。在即将发布的 0.17 版本中,我们已更改 Application Insights 代码以不再处理此事件,您应该不会再看到此问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-22
    • 2014-09-09
    • 2012-06-09
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多