Unable to make the <HttpResponseMessage> task "wait"

I am trying to write a method in my web-api shell. I want to use the "async / await" function so that the user interface is not blocked. Below is a snippet of code in a web api shell.

    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 that I get a compiler error in the instructions below.

HttpResponseMessage response = await client.GetAsync(absoluteUrl);

It says "Type System.Threading.Tasks.Task <System.Net.Http.HttpResponseMessage> is not awaitable". After a long search, I can not get rid of this error. Any ideas I'm wrong about? Please, help.

+4
source share
3 answers

, , Task, Task<T>. return await response.Content.ReadAsAsync<T>(). Task<T>:

public static async Task<T> Get<T>(...)
+4

... , .

  • web api 2.2 dll system.net.http.formatting

enter image description here

  1. Task Task<T>

!

+1

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

0
source

Source: https://habr.com/ru/post/1542903/


All Articles