WebAPI Response never completes when returning HttpResponseMessage and Application Insights is configured

倾然丶 夕夏残阳落幕 提交于 2019-12-10 16:13:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!