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.

1
  • As far as debugging async methods with breakpoints goes, it's somewhat unpredictable, so I wouldn't rely on what you observed in a debugger. Commented May 24, 2017 at 10:55

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank You. It worked. Now i will try to figure why. Have a nice day! :)
@OskarS: The key guideline is don't block on async code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.