How to dispose variables in Custom Http Client Handler?

Tanul 1,251 Reputation points
2021-02-04T20:18:25.677+00:00

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.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,282 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,479 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,136 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-02-04T22:39:28.067+00:00

    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://learn.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2017