OskarS
OskarS

Reputation: 65

Async method calling async method and awaiting another method (http client)

I have some problem with async method.

public async void MakePost()
    {
        var cookieArray =  GetCookies().Result;
       (...)
    }
async public Task<string[]> GetCookies()
    {
        (...)
        var response = await httpClient.SendAsync(request);
        string cookieTempSession = response.Headers.ToString();
        (...)
        return cookieArray;
    }

Nothing happening after var response = await httpClient.SendAsync(request); I put breakpoint in next line string cookieTempSession = response.Headers.ToString(); but it never reach it. I tried to "try catch" but also nothing happend. When I merge this two methods into one it works perfect but it's not so pretty. I just wondering what happened there.

Upvotes: 2

Views: 190

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726669

Since the first method is async, you should use await instead of Result:

var cookieArray = await GetCookies();

If you are not programming front end, add ConfigureAwait(false) (why?) to the call, like this:

var cookieArray = await GetCookies().ConfigureAwait(false);
...
var response = await httpClient.SendAsync(request).ConfigureAwait(false);

Upvotes: 1

Related Questions