Home > Net >  HttpClient GetDiscoveryDocumentAsync() Method hanging
HttpClient GetDiscoveryDocumentAsync() Method hanging

Time:02-10

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:

var client = new HttpClient();
        var disco = await client.GetDiscoveryDocumentAsync("https://icdaccessmanagement.who.int");
        if (disco.IsError) return null;

        var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
        {
            Address = disco.TokenEndpoint,
            ClientId = clientId,
            ClientSecret = clientSecret,
            Scope = "icdapi_access",
            GrantType = "client_credentials",
            ClientCredentialStyle = ClientCredentialStyle.AuthorizationHeader
        });

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

CodePudding user response:

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

  •  Tags:  
  • Related