64

I used quote marks around "right way" because I'm already well aware that the right way to use an asynchronous API is to simply let the asynchronous behavior propagate throughout the entire call chain. That's not an option here.

I'm dealing with a very large and complicated system designed specifically to do batch processing synchronously in a loop.

The reason why suddenly I'm using HttpClient is because prior to now all data for the batch processing was gathered from a SQL database, and now we're adding a Web API call to the mix.

Yes, we're calling a Web API in a synchronously executing loop. I know. Rewriting the whole thing to be async just isn't an option. This is actually what we want to do. (We're minimizing the number of API calls as much as possible)

I actually did try to propagate the async behavior up the call chain, but then I found myself 50 files deep in changes, still with hundreds of compiler errors to resolve, and lost all hope. I am defeated.

So then, back to the question, given Microsoft's recommendation to never use WebRequest for new development and to instead use HttpClient, which offers only an asynchronous API, what am I to do?

Here is some pseudo-code of what I'm doing...

foreach (var thingToProcess in thingsToProcess)
{
    thingToProcess.ProcessStuff(); // This makes an API call
}

How do I implement ProcessStuff()?

My first implementation looked like this

public void ProcessStuff()
{
    var apiResponse = myHttpClient // this is an instance of HttpClient
        .GetAsync(someUrl)
        .Result;

    // do some stuff with the apiResponse
}

I was told however, that calling .Result in this manner can result in deadlocks when it's called from something like ASP.NET due to the synchronization context.

Guess what, this batch process will be kicked off from an ASP.NET controller. Yes, again, I know, this is silly. When it runs from ASP.NET it's only "batch processing" one item instead of the whole batch, but I digress, it still gets called from ASP.NET and thus I'm concerned about deadlocks.

So what's the "right way" to handle this?

spoonraker
  • 877
  • 1
  • 8
  • 12
  • 1
    Possible duplicate of: https://stackoverflow.com/questions/22628087/calling-async-method-synchronously – Paul Ballew Nov 28 '18 at 22:37
  • Possible duplicate of [Calling async method synchronously](https://stackoverflow.com/questions/22628087/calling-async-method-synchronously) – soccer7 Nov 28 '18 at 22:41

3 Answers3

82

Try the following:

var task = Task.Run(() => myHttpClient.GetAsync(someUrl)); 
task.Wait();
var response = task.Result;

Use it only when you cannot use an async method.

This method is completely deadlock free as mentioned on the MSDN blog: ASP.Net–Do not use Task .Result in main context.

Wai Ha Lee
  • 8,173
  • 68
  • 59
  • 86
soccer7
  • 2,832
  • 2
  • 25
  • 48
  • 4
    Thank you for finally providing a definitive answer to this in the form of the MSDN post. I actually Googled this before posting and found many similar questions on StackOverflow, but none of them provided a source from MS that explicitly stated that running the task on a thread, waiting, and fetching the result after waiting is safe. Many other SO threads on this question actually have the wrong answer as the accepted answer, but it looks very similar to the right answer. That is, they skip the call to .Wait() – spoonraker Nov 29 '18 at 16:03
  • 4
    This doesn't work. Deadlock on `task.Wait();` – Joshua Feb 16 '21 at 20:07
  • 2021 update: See the other answer from @alexs which I have now marked as the accepted answer. There's a new synchronous method added to the HttpClient API. – spoonraker Mar 17 '21 at 14:15
  • 10
    @spoonraker I think this is still the better answer for the specific question you asked, which is likely to draw people for whom "use .NET 5" is not a reasonable answer to get one little piece of code working. For them, the fundamental premise of the question is still very relevant: there is no synchronous API, but the alternatives are deprecated. I doubt many people using .NET 5 would even have a reason to ask this question. Just a thought. – Daniel Apr 28 '21 at 19:07
  • Might be obvious for some, but wasn't for me and wanted to note for others just in case - add using System.Threading.Tasks; to your code. I chose the wrong suggested reference and wanted to save someone else the confusion. – Victor Lockwood Nov 17 '21 at 22:01
34

For anyone coming across this now, .NET 5.0 has added a synchronous Send method to HttpClient. https://github.com/dotnet/runtime/pull/34948

You can therefore use this instead of SendAsync. For example

public string GetValue()
{
    var client = new HttpClient();
            
    var webRequest = new HttpRequestMessage(HttpMethod.Post, "http://your-api.com")
    {
        Content = new StringContent("{ 'some': 'value' }", Encoding.UTF8, "application/json")
    };

    var response = client.Send(webRequest);

    using var reader = new StreamReader(response.Content.ReadAsStream());
            
    return reader.ReadToEnd();
}

This code is just a simplified example, it's not production ready.

alexs
  • 871
  • 12
  • 14
1

You could also look at using Nito.AsyncEx, which is a nuget package. I've heard of issues with using Task.Run() and this this addresses that. Here's a link to the api docs: http://dotnetapis.com/pkg/Nito.AsyncEx/4.0.1/net45/doc/Nito.AsyncEx.AsyncContext

And here's an example for using an async method in a console app: https://blog.stephencleary.com/2012/02/async-console-programs.html