HttpRequestException on published ASP.NET Core application

42 Views Asked by At

I have an ASP.NET Core MVC and Web API combined project that's published to IIS. Whenever an API call is made using HttpClient, I get a status 500 on the webpage. Logging the exception shows:

System.Net.Http.HttpRequestException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. This exception occurs on 'await httpClient.GetAsync("url")'

I'm not sure if this is an server error or something with the application code? I have tried adding cors, Increasing request time-out. When I call the API endpoint using Postman, I get an correct response back. I use .NET 7 and Entity Framework Core v7.0.10

This is how I setup HttpClient and make API calls:

builder.Services.AddHttpClient("MyClient", client =>
{
    var configuration = builder.Configuration;

    client.BaseAddress = new Uri(configuration["ConnectionStrings:BaseURL"]);
    client.DefaultRequestHeaders.Add(
        configuration["Authentication:APIKeyHeaderName"],
        configuration["Authentication:APIKey"]);
});
using(var httpClient = _httpClientFactory.CreateClient("MyClient"))
{
    var response = await httpClient.GetAsync("/api/endpoint-name");

    if (response.IsSuccessStatusCode)
    {
        ....
    }
}
0

There are 0 best solutions below