问题
I have an MVC5/WebAPI2 application which has had Application Insights enabled since I created the web project.
WebApi methods that return objects (e.g. string, model objects) are returned as expected - serialized into JSON or 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;
}
}
When I created an action that returns an HttpResponseMessage, I noticed a strange behavior in the browser (tested with Chrome and IE). Specifically, my content was returned to the browser, but the "busy" indicator never stopped spinning until I hit the Stop button, or stopped the web server (Visual Studio 2013).
I've verified that the method above works as expected in a web app without Application Insights. Specifically, once the data is returned to the browser, the response ends. When the method below is added to a freshly-created app with Application Insights, the behavior described previously is encountered.
Any thoughts?
回答1:
This is caused by a NullReferenceException
thrown in the ASP.NET request pipeline. The Microsoft.ApplictionInsights.Web package is handling the HttpApplication.PreSendRequestHeaders event, which triggers the problem. In the upcoming 0.17 release we have changed the Application Insights code to no longer handle this event and you shouldn't see this problem anymore.
来源:https://stackoverflow.com/questions/29270440/webapi-response-never-completes-when-returning-httpresponsemessage-and-applicati