Retry policy in HttpClientManagementConnection (Auth0 .NET SDK)

Hi,

I’m looking into using the Auth0 .NET SDK to access the management API. The operations that I’ll need to access are get user and create user for now. Looking at the SDK documentation: Documentation I’m wondering if it’s possible to apply Polly retry policy to the HttpClient used by the HttpClientManagementConnection. From this documentation, Class HttpClientManagementConnection it seems that the constructor receives an HttpClient object.

I wonder if the following code on Startup would enforce retry policy to the HttpClient used by the HttpClientManagementConnection. It uses the AddHttpClient extension from .NET Core:

services.AddSingleton<IManagementConnection, HttpClientManagementConnection>();
services.AddHttpClient<IManagementConnection, HttpClientManagementConnection>().AddPolicyHandler(GetRetryPolicy());

I’m not sure how to test if the retry is working

Will try to research that but I’m not .NET guy, maybe someone else from the community will be able share some tips

1 Like

I did more test and apparently injecting it like that doesn’t work. There’s no retry happening… i need to figure out how to inject the HttpClient into ManagementConnection in another way

1 Like

Using Func in dependency injection and injecting the HttpClient there seems to work

services.AddTransient<IUserProvider, UserProvider>();
services
                .AddHttpClient(UserProviderHttpClientName)
                .AddPolicyHandler((services, request) => GetRetryPolicy<UserProvider>(services, request));
services.AddTransient<IManagementConnection>((serviceProvider) =>
{
                var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
                var httpClient = httpClientFactory.CreateClient(UserProviderHttpClientName);
                return new HttpClientManagementConnection(httpClient);
});
1 Like

Glad you have figured it out and thanks for sharing with the rest of community!