Search code examples
c#asp.net-coreasync-awaitdotnet-httpclient

HttpClient in ASP.NET Core razor async stuck


I have a problem using ASP.NET Core with app hosted in IIS (not in IIS Express)...

This is my configuration:

Startup:

services.addHttpClient()

Page:

private readonly IHttpClientFactory _clientFactory;

public testModel(IHttpClientFactory clientFactory)
{
    _clientFactory = clientFactory;
}

public async void OnGet()
{
    var client = _clientFactory.CreateClient();
    client.BaseAddress = new Uri("http://localhost:37199/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // GET Method  
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "apiprueba/test");
    var response = await client.SendAsync(request);            
}

The problem: when I execute the page, I never get a response of the page (test). It only happens with IIS - not with IIS Express. What could be the problem?

Update: New test..no response using simple asp core console. Code

static async System.Threading.Tasks.Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var client = new HttpClient();
            client.BaseAddress = new Uri("http://127.0.0.1:88/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //GET Method  
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "api/mensaje");
            client.Timeout = TimeSpan.FromSeconds(30);
            var response = await client.SendAsync(request).ConfigureAwait(false);
            Console.WriteLine(response.ToString());
            Console.WriteLine("Finished");
        }

What it means: Result: local maquine: error is shown after 5 seconds.. Server: Hello world! _ ...code still running after 10 minutes..no answer error or finish the process..

it seems the server is the problem...not sure why

enter image description here

Update: after reviewed the server ..WinHttp services was stopped...was getting error if I tried to start manually..restarting the server resolved the problem..services start normal and request from app was correct..


Solution

  • WinHttp services was stopped..unable to run manually.. after restarting the server the services start correctly and all programs and test was successfull.