Can you use HttpClientFactory in Maui #3201
Replies: 4 comments 11 replies
-
I stumbled across the same question and found this blog about it. It says that you should not use HttpClientFactory and also mentions other best practices about HttpClient The linked side contained malware so i post the contents here for save reference
|
Beta Was this translation helpful? Give feedback.
-
This helpmed me USE IHttpClientFactory - that blog misleading - my app was using so much CPU and Memory - once I used IHttpClientFactory it is fixed. I use HttpClient a lot in my app. Do this builder.Services.AddHttpClient(); to your public static class MauiProgram { public static MauiApp CreateMauiApp() One of your content page get it and set it to one public static variable in you such as Utilities.cs like and use it wherever you need HttpClient _httpClient; |
Beta Was this translation helpful? Give feedback.
-
This is my approach. I create a base API class - sealed class BaseApiService : IBaseApiService
{
public BaseApiService()
{
// Link, using resilience with static clients - https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines#resilience-with-static-clients
ResiliencePipeline<HttpResponseMessage> retryPipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
.AddRetry(new MobileHttpRetryStrategyOptions())
.Build();
#pragma warning disable EXTEXP0001
ResilienceHandler resilienceHandler = new(retryPipeline)
{
InnerHandler = new SocketsHttpHandler(),
};
Client = new HttpClient(resilienceHandler)
{
BaseAddress = new Uri("https://YourApiEndpoint.com/"),
Timeout = TimeSpan.FromSeconds(10)
};
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Client.DefaultRequestHeaders.Add("User-Agent", DeviceInfo.Platform.ToString());
}
public HttpClient Client { get; private set; }
}
// Link, copied from Maui Community Toolkit - https://github.com/CommunityToolkit/Maui/pull/1643
sealed class MobileHttpRetryStrategyOptions : HttpRetryStrategyOptions
{
public MobileHttpRetryStrategyOptions()
{
BackoffType = DelayBackoffType.Exponential;
MaxRetryAttempts = 3;
UseJitter = true;
Delay = TimeSpan.FromSeconds(2);
}
} That is then registered as a singleton - builder.Services.AddSingleton<IBaseApiService, BaseApiService>(); And then can be injected and reused in any other class |
Beta Was this translation helpful? Give feedback.
-
For those who might be still confused, I think Milan Jovanović did a good job breaking the concept down in a simple fashion on his blog. You should also check this article out where the author layout a strong argument, supported by documentation, against using HttpClientFactory in Mobile applications. Weigh out your options, and make an informed decision. |
Beta Was this translation helpful? Give feedback.
-
Hi wondering if there is any example of using HttpClientFactory in Maui?
Can I do the same as I would do for a .net 5/6 webApi and create Named/Typed Clients?
Many thanks
Beta Was this translation helpful? Give feedback.
All reactions