If this pattern is correct then is there any way to debug it. Kindly suggest.
You could use local IIS on your development machine.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Team,
I have made an api in .net core 3.1 and created a custom HTTP Client Handler to manage dynamic certificates per http call but unable to understand the procedure to write and execute dispose pattern.
Startup.cs-->
services.AddHttpClient(Configuration["ClientName"], c =>
{
c.BaseAddress = new Uri(Configuration["URL"]);
}).ConfigurePrimaryHttpMessageHandler<RegistryHttpHandler>()
service.AddSingleton<IHttpClient, HttpClient>(); //My custom http client class
service.AddTransient<RegistryHttpHandler>(); // Here life time doesn't matter because Http Client is singleton
Custom Handler-->
public class RegistryHttpHandler : HttpClientHandler, IDisposable
{
private readonly IConfiguration _configuration;
private readonly Dictionary<string, X509Certificate2> _certMap;
private bool disposedValue;
//removed other code as not required here
protected override void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_certMap.Values.All(x =>
{
x.Dispose();
return true;
});
_certMap.Clear();
}
base.Dispose();
disposedValue = true;
}
}
public new void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
As far as I know, dependency injection itself disposes the objects in best possible manner and there is no need to call dispose explicitly. But, here dispose of _certmap dictionary is necessary because the value of X509certificate2 must dispose.
Could anyone please confirm how to dispose this dictionary variable in this case. I've added base.Dispose() so that if dependency injection internally execute my overriden IDisposable then at least it shall dispose http client handler as well.
If this pattern is correct then is there any way to debug it. Kindly suggest.
If this pattern is correct then is there any way to debug it. Kindly suggest.
You could use local IIS on your development machine.