0

I had this code working in .Net Core. We realized Core wasn't the best fit for us right now, so I have been refactoring in .Net 4.5.2 and when I call

HttpResponseMessage response = await client.GetAsync(passedlURI);

It just continues thinking indefinitely. I have pointed it at a local host version of my API and confirmed that the API is reading the header and returning my data correctly, I have used Postman to confirm that both the live API and localhost API are sending me the correct data. Here is my code so far:

enter image description here

7
  • I wonder if this Code compiles...Anyway!? Further this architecture doesn't make sense to me. Commented Apr 11, 2017 at 18:43
  • Can you turn public string CallAPI into public async Task<string> CallAPI? Commented Apr 11, 2017 at 18:43
  • 1
    @DotNetDev You don't need a return statement in an async Task method. Only async Task<T>. Commented Apr 11, 2017 at 18:43
  • Right, it's void, thx Commented Apr 11, 2017 at 18:45
  • Why did you use three methods here? Looks like they can all be combined into one. And you don't need any fields, just use local variables. Commented Apr 11, 2017 at 19:20

1 Answer 1

8

Make sure to use .ConfigureAwait(false) with each of your await. Waiting synchronously on a task that internally awaits on the current synchronization context is a well-known cause of deadlocks in ASP.NET.

For instance, instead of:

await GetAPIAsync();

Do:

await GetAPIAsync().ConfigureAwait(false);

Of course, it would be even better if you didn't wait synchronously on a task to begin with.

The issue does not happen on ASP.NET Core because it doesn't use a custom synchronization context.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.