I have a .NET Core class library. Am creating HttpClient instance using IHttpClientFactory without dependency injection. I have included Microsoft DI nuget package in my class library.
Sample Code #1:
Class A {
private readonly HttpClient client;
public A(){
var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
var _httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
client = _httpClientFactory.CreateClient(); //HttpClient instance created
//TODO: Add custom message handler without DI.
}
}
Using DI, we can configure custom message handlers: Sample code #2 with DI:
services.AddHttpClient()
.ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (m, crt, chn, e) => true
};
});
I want to add HttpClientHandler to my Sample code #1 without DI. How to configure primary message handler without DI?