4

I am trying to write a method in my web api wrapper. I want to make use of "async/await" feature so that UI doesn't get blocked. Below is the code snippet in the web api wrapper.

 public static async Task Get<T>(Dictionary<string, string> paramDictionary, string controller) { try { string absoluteUrl = BaseUrl + controller + "?"; absoluteUrl = paramDictionary.Aggregate(absoluteUrl, (current, keyValuePair) => current + (keyValuePair.Key + "=" + keyValuePair.Value + "&")); absoluteUrl = absoluteUrl.TrimEnd('&'); using (HttpClient client = GetClient(absoluteUrl)) { HttpResponseMessage response = await client.GetAsync(absoluteUrl); return await response.Content.ReadAsAsync<T>(); } } catch (Exception exception) { throw exception; } } 

The problem is I get compiler error at the statement below.

HttpResponseMessage response = await client.GetAsync(absoluteUrl); 

It says "Type System.Threading.Tasks.Task <System.Net.Http.HttpResponseMessage> is not awaitable". After much searching I am not able to get rid of this error. Any ideas where I am going wrong? Please help.

10
  • 4
    side note: throw exception; will destroy the callstack. Just use throw; Commented Jun 2, 2014 at 11:10
  • I tried the code in the using block and the first line compiles, but the compiler didn't find the definition of ReadAsAsync<T>. Is that an extension method? Commented Jun 2, 2014 at 11:12
  • @jgauffin yeah an in addition to that it's not really necessary to catch an exception just for re-throwing it. just remove the whole try catch part here... Commented Jun 2, 2014 at 11:12
  • 2
    Are you targeting .Net 4.0 in VS2013? The version of Task in .Net 4.0 does not have the required GetAwaiter method so it cannot be used with async/await. You can use the async targeting pack to add the required methods. Commented Jun 2, 2014 at 11:32
  • 1
    @NedStoyanov, yep it does, same as as try/catch/finally and other C# control flow statements. Although, you cannot await inside catch/finally (you can inside try). Commented Jun 2, 2014 at 11:53

3 Answers 3

5

As far as I can tell, it's because your method returns Task, not Task<T>. So you cannot do return await response.Content.ReadAsAsync<T>(). Change the signature to return Task<T>:

public static async Task<T> Get<T>(...) 
Sign up to request clarification or add additional context in comments.

12 Comments

I tried your way. But didn't work unfortunately..:-(
@TejasSutar, now that you've changed the signature to Task<T>, what's the error message and what line exactly gives it?
@TejasSutar, are you indeed targeting Net 4.0?
It's the same error message I mentioned earlier. One more thing I forgot to mention that compiler cannot find definition for ReadAsAsync method. I am referring following site for code: asp.net/web-api/overview/web-api-clients/…
Yes. I just added Microsoft.Bcl.Async using package manager in VS. Now I need to get rid of compiler error at the statement "return await response.Content.ReadAsAsync<T>();" Thanks for help @Noseratio.
|
1

Hmmm... I did the following to get it compile.

  1. Add web api 2.2 for the system.net.http.formatting extension dll

enter image description here

  1. Change the method signature from Task to Task<T>

Good luck!

Comments

0

Updating the Microsoft.Bcl.Build Nuget package worked for me.

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.