返回HttpResponseMessage并配置Application Insights时,WebAPI响应永远不会完成

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

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

public class TestController : ApiController { [HttpGet] [AllowAnonymous] async public Task 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)。

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

有什么想法吗?

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