Blazor HttpClientHandler times out

Sandra Anderson 46 Reputation points
2023-05-31T05:47:40.13+00:00

I'm not sure if this is the correct area to ask this question so if it's not please can you point me to the right place....

I've got a Blazor server app and it uses HttpClientHandler and IHttpClientFactory  to call the Data API. When my app is deployed on an on-premise IIS server, it stops working quite frequently. I have worked out this is because the http clients are being disposed - 'System.Net.Http.SocketsHttpHandler' "Cannot access a disposed object".

I think something is wrong with my coding pattern but after hours of reading, cannot quite pin-point why this is.

My code in the Program.cs file (start up) and data service is as follows:

Thanks in advance for any help!

//Program.cs file

//create handler
HttpClientHandler handler = new HttpClientHandler()
{
    UseDefaultCredentials = true,
    AllowAutoRedirect = false,
};


//create named http client
string uriPath = "https://...";

services.AddHttpClient("myHttpClient", client =>
{
    client.BaseAddress = new Uri(uriPath);
    client.DefaultRequestHeaders.Clear();
    client.DefaultRequestHeaders.Add(HeaderNames.Accept, "application/json");
})
    .ConfigurePrimaryHttpMessageHandler(() => handler);




//Data service code

//injection of IHttpClientService
private readonly IHttpClientService _httpClientService;

public SomeDataService(IHttpClientService clientService)
{
	_httpClientService = clientService;
}

//then within the data service method

//create http client 
var client = _httpClientService.GetHttpClientFactory("myHttpClient");
.... await client.GetStreamAsync($"api/....")...;
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,497 questions
{count} votes

Accepted answer
  1. JasonPan - MSFT 4,811 Reputation points Microsoft Vendor
    2023-05-31T07:27:19.5566667+00:00

    Hi @Sandra Anderson,

    I found this link, and I think the answer does a good job of explaining why *'System.Net.Http.SocketsHttpHandler' "Cannot access a disposed object"* error occurs.

    Could you change the code like below and tell us it works or not.

    services.AddHttpClient("myHttpClient", client =>
    {
        client.BaseAddress = new Uri(uriPath);
        client.DefaultRequestHeaders.Clear();
        client.DefaultRequestHeaders.Add(HeaderNames.Accept, "application/json");
    })
    .ConfigurePrimaryHttpMessageHandler(() =>
    {
        return new HttpClientHandler()
        {
            UseDefaultCredentials = true,
            AllowAutoRedirect = false
        };
                
    }); 
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Jason Pan

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful