I am writing an application that proxies some HTTP requests using the ASP.NET web API, and I am struggling to determine the source of the intermittent error. It looks like a race condition ... but I'm not quite sure.
Before I explain in more detail, this is the general communication flow of the application:
- The client makes an HTTP proxy request 1 .
- Proxy 1 redirects the contents of the HTTP request to Proxy 2
- Proxy 2 transfers the contents of the HTTP request to the target web application
- The target web application responds to the HTTP request and the response is transmitted (transmitted over the channels) to Proxy 2
- Proxy 2 returns the response of Proxy 1 , which in turn answers the original call by the Client .
Proxy applications are written to the ASP.NET RTM web API using .NET 4.5. The code for running the relay looks like this:
//Controller entry point. public HttpResponseMessage Post() { using (var client = new HttpClient()) { var request = BuildRelayHttpRequest(this.Request); //HttpCompletionOption.ResponseHeadersRead - so that I can start streaming the response as soon //As it begins to filter in. var relayResult = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).Result; var returnMessage = BuildResponse(relayResult); return returnMessage; } } private static HttpRequestMessage BuildRelayHttpRequest(HttpRequestMessage incomingRequest) { var requestUri = BuildRequestUri(); var relayRequest = new HttpRequestMessage(incomingRequest.Method, requestUri); if (incomingRequest.Method != HttpMethod.Get && incomingRequest.Content != null) { relayRequest.Content = incomingRequest.Content; } //Copies all safe HTTP headers (mainly content) to the relay request CopyHeaders(relayRequest, incomingRequest); return relayRequest; } private static HttpRequestMessage BuildResponse(HttpResponseMessage responseMessage) { var returnMessage = Request.CreateResponse(responseMessage.StatusCode); returnMessage.ReasonPhrase = responseMessage.ReasonPhrase; returnMessage.Content = CopyContentStream(responseMessage); //Copies all safe HTTP headers (mainly content) to the response CopyHeaders(returnMessage, responseMessage); } private static PushStreamContent CopyContentStream(HttpResponseMessage sourceContent) { var content = new PushStreamContent(async (stream, context, transport) => await sourceContent.Content.ReadAsStreamAsync() .ContinueWith(t1 => t1.Result.CopyToAsync(stream) .ContinueWith(t2 => stream.Dispose()))); return content; }
Error that occurs intermittently:
The asynchronous module or handler is completed while the asynchronous operation has not yet been completed.
This error usually occurs during the first few requests to proxy applications, after which the error no longer appears.
Visual Studio never catches an exception on a throw. But the error can be detected in the Global.asax Application_Error event. Unfortunately, there is no stack trace in Exception.
Proxy applications are hosted in Azure Web roles.
Any help identifying the culprit would be appreciated.
Gavin Osborn Feb 25 '13 at 4:38
source share