2

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.

OskarS
  • 65
  • 8

1 Answers1

1

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);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523