I’m currently facing an issue configuring my HttpClient in DI for an Android MAUI app.
My current configuration is like this:
public static MauiApp CreateMauiApp()
{
MauiAppBuilder builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
builder.Services.AddHttpClient<IEventService, EventService>(config =>
{
config.BaseAddress = new Uri("http://010.00.0.000:432/api/v1");
})
.ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler {
PooledConnectionLifetime = TimeSpan.FromMinutes(15) })
.SetHandlerLifetime(Timeout.InfiniteTimeSpan);
builder.Services.AddSingleton<EventSelectionViewModel>();
builder.Services.AddSingleton<EventSelectionPage>();
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
To avoid DNS issue in singleton VM i set the primary handler as SocketsHttpHandler with PooledConnectionLifetime set and recycling disable at IHttpClientFactory by setting HandlerLifetime.
Now what i’m trying to do knowing i’m currently working on android emulator is adding an additional AndroidHttpHandler to be able to request my local service on emulator:
public class HttpsClientHandlerService
{
public static HttpMessageHandler GetPlatformMessageHandler()
{
AndroidMessageHandler handler = new()
{
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
{
if (cert != null && cert.Issuer.Equals("CN=localhost"))
return true;
return errors == System.Net.Security.SslPolicyErrors.None;
}
};
return handler;
}
}
public class HttpAndroidPlatformHandler : DelegatingHandler
{
public HttpAndroidPlatformHandler(HttpMessageHandler innerHandler) : base(innerHandler) { }
}
And the builder part:
builder.Services.AddHttpClient<IEventService, EventService>(config =>
{
config.BaseAddress = new Uri("http://010.00.0.000:432/api/v1");
})
.ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler { PooledConnectionLifetime = TimeSpan.FromMinutes(15) })
.ConfigureAdditionalHttpMessageHandlers((handlerList, _) => handlerList.Insert(0, new HttpAndroidPlatformHandler(HttpsClientHandlerService.GetPlatformMessageHandler())))
.SetHandlerLifetime(Timeout.InfiniteTimeSpan);
builder.Services.AddSingleton<EventSelectionViewModel>();
builder.Services.AddSingleton<EventSelectionPage>();
I’m getting an:
System.InvalidOperationException: ‘The ‘InnerHandler’ property must be null. ‘DelegatingHandler’ instances provided to
‘HttpMessageHandlerBuilder’ must not be reused or cached.
Handler: ‘ESC.EventTracking.App.Platforms.Android.HttpAndroidPlatformHandler”
From the doc I read, the innerHandler of the constructor should be the parent handler in the chain, in this case it would be:
1.SocketsHttpHandler -> 2.AndroidMessageHandler(with SocketsHttpHandler as
innerHandler)
So is it even possible to combine this two handler in an HttpClient instance ?
If yes how could i perform this?
Thanks in Advance