question

TaB-8489 avatar image
1 Vote"
TaB-8489 asked TaB-8489 commented

How to dispose variables in Custom Http Client Handler?

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.


dotnet-csharpdotnet-runtimedotnet-aspnet-core-generaldotnet-runtime-core
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered TaB-8489 commented

If this pattern is correct then is there any way to debug it. Kindly suggest.

You could use local IIS on your development machine.

https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2017

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@DuaneArnold-0443 , Thanks for sharing but is this dispose pattern correct to dispose client handler and dictionary variable. Or is there any possible way to handle this stuff internally with the help of Dependency injunction.

0 Votes 0 ·

If what you have done works for you, then use it. I can't tell you if what you have done is good or bad, since I seldom use Dispose().

0 Votes 0 ·
TaB-8489 avatar image TaB-8489 DuaneArnold-0443 ·

No issue. Thanks :)

0 Votes 0 ·