There is an extension method that registers IAccountSearchServiceClient
with some policy handlers looks like Polly lib is used
public static IServiceCollection AddAccountSearchServiceClient(this IServiceCollection services)
{
services.AddHttpClient<AccountSearchServiceClient>()
.ConfigureHttpClient((sp, client) =>
{
var options = sp.GetRequiredService<IOptions<AccountSearchSettings>>();
var settings = options.Value;
client.BaseAddress = settings.BaseUrl;
})
.ConfigurePrimaryHttpMessageHandler(() =>
{
var handler = new HttpClientHandler
{
ClientCertificateOptions = ClientCertificateOption.Manual,
ServerCertificateCustomValidationCallback = (m, c, cc, pe) => true
};
return handler;
})
.AddPolicyHandler(request => request.Method == HttpMethod.Get ? Policies.ShortTimeout : Policies.LongTimeout)
.AddPolicyHandlerFromRegistry("circuitBreaker")
.AddPolicyHandlerFromRegistry("bulkhead")
.AddPolicyHandlerFromRegistry("RetryPolicy");
services.AddScoped<IAccountSearchServiceClient>(sp => sp.GetRequiredService<AccountSearchServiceClient>());
return services;
}
at runtime getting such a DI error:
System.InvalidOperationException HResult=0x80131509 Message=No service for type 'Polly.Registry.IReadOnlyPolicyRegistry`1[System.String]' has been registered.
Source=Microsoft.Extensions.DependencyInjection.Abstractions
the error occurs here
sp.GetRequiredService<AccountSearchServiceClient>()
I'm not very familiar with Polly. Is there something missing?
I've put a break point on a constructor but the ctor is not called error happens earlier after ConfigurePrimaryHttpMessageHandler
the consturctor looks as the following:
public AccountSearchServiceClient(HttpClient httpClient, IOptions<AccountSearchSettings> settings)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings));
}
no direct injections or usages of IReadOnlyPolicyRegistry
I guess it's something internal type of Polly
It turned out the missing part was the policy registration: