none
Help with HttpClient GetAsync syntax RRS feed

  • Question

  • I'm trying to call GetAsync on my HttpClient object but the call just hangs forever.

    This is the (slightly obfuscated)  URL that works from an internet browser:

    http://HostingMachine/TheService/ThePage.asmx/HasAdminRights?user=RHaggard&computer=WS-RHAGGARD

    My WPF C# test program is very simple. It has an HttpClient object that is named httpClient and defined as a static member. In the class's static constructor httpClient is instantiated and its BaseAddress member is loaded with a Uri containing http://HostingMachine/TheService/ThePage.asmx .

    httpClient.DefaultRequestHeaders.Accept.Clear();
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
    string user = Environment.GetEnvironmentVariable("USERNAME");
    string computer = Environment.GetEnvironmentVariable("COMPUTERNAME");
    
    // This string will look like this:
    // HasAdminRights?user=RHaggard&computer=WS-RHAGGARD
    string subAddr = string.Format("{0}?user={1}&computer={2}", func, user, computer);
    
    // This is where the program stalls.
    // It never returns from the GetAsync.
    HttpResponseMessage resp = await httpClient.GetAsync(subAddr);
    if (resp.IsSuccessStatusCode)
    {
        string strResp = await resp.Content.ReadAsStringAsync();
    }
    


    Richard Lewis Haggard

    Wednesday, September 11, 2019 4:15 PM

Answers

  • Ah. I understand. Deadlock. Nothing at all to do with HttpClient. Unstated in the original post was the fact that this call is being made as a result of a menu item click. Its handler eventually gets to this:

    public static bool HasAdminRights()
    {
        return HasRights( Rights.Admin, ConstHasAdminRights).Result;
    }
    

    This click handler calls HasRights which has a signature that looks like this:

    private static async Task<boolHasRightsRights rightstring func )
    

    Now the gun is loaded. The trigger gets pulled when this code is called:

    HttpResponseMessage resp = await httpClient.GetAsync(subAddr);

    Behind the scenes the code called by GetAsync runs to completion BUT it needs to restore a context which is locked up because the caller used HasRights(...).Result. It has the context locked up and is waiting for the GetAsync to complete. Oops. Deadlock.

    A hack, and I don't like it but it works, is to change the await line to

    string resp = await httpClient.GetStringAsync(subAddr).ConfigureAwait(false);


    Richard Lewis Haggard

    • Marked as answer by Richard.Haggard Wednesday, September 11, 2019 6:54 PM
    Wednesday, September 11, 2019 6:53 PM

All replies

  • Hello Richard,

    To see what is transpiring during the GetAsync method use Progress/Telerik Fiddler (it's totally) free or use a similar tool to see status code for the request.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, September 11, 2019 4:29 PM
    Moderator
  • Ah. I understand. Deadlock. Nothing at all to do with HttpClient. Unstated in the original post was the fact that this call is being made as a result of a menu item click. Its handler eventually gets to this:

    public static bool HasAdminRights()
    {
        return HasRights( Rights.Admin, ConstHasAdminRights).Result;
    }
    

    This click handler calls HasRights which has a signature that looks like this:

    private static async Task<boolHasRightsRights rightstring func )
    

    Now the gun is loaded. The trigger gets pulled when this code is called:

    HttpResponseMessage resp = await httpClient.GetAsync(subAddr);

    Behind the scenes the code called by GetAsync runs to completion BUT it needs to restore a context which is locked up because the caller used HasRights(...).Result. It has the context locked up and is waiting for the GetAsync to complete. Oops. Deadlock.

    A hack, and I don't like it but it works, is to change the await line to

    string resp = await httpClient.GetStringAsync(subAddr).ConfigureAwait(false);


    Richard Lewis Haggard

    • Marked as answer by Richard.Haggard Wednesday, September 11, 2019 6:54 PM
    Wednesday, September 11, 2019 6:53 PM