.NET Standard - issues with async/await?

wallymwallym USInsider, Beta ✭✭✭

I've been working on converting an XF project from a shared project to a .NET Standard project. I've run into some weirdness in the area of async/await and wanted to throw this out there to see if anyone can explain what I am doing wrong, what is happening, or provide some insight.

  • I'm calling a remote web service that is running .NET Core 2.1.
  • Web service is on Azure.
  • I can call the web service using async/await. The web service receives the value, performs the operation, and exits.
  • A breakpoint on the following line is never hit.

To get this to work, I ended up not using async/await but to request the .Result. By making this change, everything regarding this issue seemed to be resolved.

Below is the code that did not work:

        User res = new User();
        try
        {
            var url = CreateUrl(String.Format(UserSettingsUrlGet, UserToken));
            var httpC = new BaseHttpClient();
            var body = await httpC.GetAsync(url); // <-- this is the line that never returned via await. Using .Result resolved this.
            var str = await body.Content.ReadAsStringAsync();
            res = JsonConvert.DeserializeObject<User>(str);
            httpC.Dispose();
            return res;
        }
        catch (System.Exception exc)
        {
            System.Diagnostics.Debug.WriteLine(exc.Message);
        }
        return res;

Interestingly, in another part of the application, I had to take a .Result, convert it to call using async/await, and that worked. So, I'm really confused and starting to wonder if there is a problem in the .NET standard implementation of async/await, or if I am stupid in doing something wrong. I suspect I have done something wrong, but am not quite sure what to look for.

TIA.

Wally

Answers

Sign In or Register to comment.