'HttpClient GetDiscoveryDocumentAsync() Method hanging

In ASP.NET Project I using web service that I get data from it by APIs, I am using this code to connect with web service but it hanging:

public class HomeController : Controller
{
    public ActionResult Index()
    {

        var result = ICDa.GetCodeResult().GetAwaiter().GetResult();

        return View();
    }

    private  async Task<string> GetCodeResult()
    {
        var client = new HttpClient();
        var disco = await 
 client.GetDiscoveryDocumentAsync("https://icdaccessmanagement.who.int");
       if (disco.IsError)
       {
          return "error";
       }
       else
       {
           return "success";
       }
    }
}

But when I using this code in Console App, it work right, no problem



Solution 1:[1]

Mason's comment is correct. I try to run in console application and webapi application, all work fine.

So my test results proved that GetDiscoveryDocumentAsync() in HttpClient is normal.

Test Code in Console Application

using System;
using System.Net.Http;
using System.Threading.Tasks;

using IdentityModel.Client;

using Newtonsoft.Json.Linq;

namespace ClientCredentialsConsoleApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Task.Run(async () =>
            {
                await aa();
            }).GetAwaiter().GetResult();
        }
        public static async Task<string> aa()
        {
            var client = new HttpClient();
            var disco = await client.GetDiscoveryDocumentAsync("https://icdaccessmanagement.who.int");
            if (disco.IsError)
            {
                return "error";
            }
            else {
                return "success";
            }
        }
    }
}

And Result

enter image description here

Test Code in Webapi Application

[HttpGet("Test_HttpClient")]
public async Task<string> Test_HttpClient()
{
    string res=await aa();
    return res;
}
private  async Task<string> aa()
{
    var client = new HttpClient();
    var disco = await client.GetDiscoveryDocumentAsync("https://icdaccessmanagement.who.int");
    if (disco.IsError)
    {
        return "error";
    }
    else
    {
        return "success";
    }
}

And Result

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jason