Locking problem with MVC4 and HttpClient

Topics: ASP.NET MVC, General
Sep 11, 2013 at 1:25 PM
Hey guys,
I'm trying to use the HttpClient on my MVC4 Host project to make a web request to an external source when the user does something on the page.
Apparently the client is getting locked when i try to use a pipelined HttpClient (contains a DelegatingHandler to log the client activity). If I create an ordinary HttpClient (without any DelegatingHandler) everything works fine...

This is very awkward because i've already done the exact same on a WebAPI host (or even a Console host) without problems.

Here is the implementation of my HttpClient. I've removed all the code from my HttpLoggingHandler because apparently its irrelevant since it continues to lock down.
public class MyClientHandler : IDisposable
    {
        public HttpClient HttpClient { get; set; }

        public MyClientHandler()
        {
            this.HttpClient = HttpClientFactory.Create(new HttpLoggingHandler());
        }


        public string Search(string searchText)
        {
            var requestUri = string.Format("https://www.google.pt/#q={0}", searchText ?? string.Empty);

            var getString = this.HttpClient.GetStringAsync(requestUri);

            getString.Wait();
            return getString.Result;
        }

        public void Dispose()
        {
            if (this.HttpClient != null)
            {
                this.HttpClient.CancelPendingRequests();
                this.HttpClient.Dispose();
            }
        }
    }

    public class HttpLoggingHandler : DelegatingHandler
    {
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {

            var response = await base.SendAsync(request, cancellationToken);

            return response;
        }

    }
And this is how i am invoking it from my controller:
public class MyController : Controller
{
    (...)
    
    private static MyClientHandler handler = new MyClientHandler();

    [HttpPost]
    public ActionResult DoSomethingWithMyHandler()
    {
        handler.Search("hey");

        // If we got this far, something failed, redisplay form
        return View();
    }
}
Am i doing something wrong?

Appreciate the help,
Carlos
Coordinator
Sep 11, 2013 at 4:13 PM
You should not call getString.Wait() - this blocks the thread and risks deadlocking ASP.NET. Instead you should use the "await" keyword to await the asynchronous action.

Daniel Roth
Sep 11, 2013 at 9:20 PM
I completly agree with you. The problem is that i must make the call in a synchronous manner. Also, HttpClient does not provide synchronous methods for making webrequests..

Still, if i dont use a pipelined HttpClient, that is, if i just use the plain HttpClient without injecting any DelegatingHandlers onto it, the getString.Wait() blocks only until the task completes.
Coordinator
Sep 12, 2013 at 12:06 AM
What prevents you from making the Search API and the controller action async?

Daniel Roth