Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How to define Polly policies when working with Refit library in ASP.NET Core?
Note: this is based on my question on SO and subsequent activity there
I am trying to add resilience using Polly to HTTP calls that are performed using Refit library.
I am using the Refit factory method RestService.For<>
and I have a working code, but I feel it is way more complex than it should be:
The refit interface
[Headers("Authorization: Bearer")]
public interface IBarIntegration
{
[Get("/api/ext/Foo/GetFooBriefInfo")]
Task<ApiResponse<GetFooBriefInfoForFooDto>> GetFooBriefInfo(GetFooBriefInfoForFooInputDto inputData);
}
Factory class that also configures the authentication
internal sealed class BarApiClientHelper : IBarApiClientHelper
{
private readonly IOptionsSnapshot<BarApiSettings> _BarSettings;
private readonly IAccessTokenHelperService _accessTokenHelperService;
public BarApiClientHelper(IOptionsSnapshot<BarApiSettings> BarSettings, IAccessTokenHelperService accessTokenHelperService)
{
_BarSettings = BarSettings;
_accessTokenHelperService = accessTokenHelperService;
}
public async Task<TApiClient> CreateApiClient<TApiClient>(CancellationToken token)
{
string baseUrl = _BarSettings.Value.BaseUrl;
string accessToken = await _accessTokenHelperService.GetAccessToken(token);
var refitClient = RestService.For<TApiClient>(baseUrl, new RefitSettings
{
AuthorizationHeaderValueGetter = () => Task.FromResult(accessToken)
});
return refitClient;
}
}
Example call
public async Task<GetFooBriefInfoForFooDto> GetFooBriefInfo(string upn, CancellationToken token)
{
var apiClient = await _clientHelper.CreateApiClient<IBarIntegration>(token);
var retrialPolicy = _registry.Get<AsyncRetryPolicy<IApiResponse>>(DefaultRetrialPolicy);
var func = async () => (IApiResponse) await apiClient.GetFooBriefInfo(new GetFooBriefInfoForFooInputDto { FooContactUpn = upn });
var FooInfo = (ApiResponse<GetFooBriefInfoForFooDto>) await retrialPolicy.ExecuteAsync(func);
await FooInfo.EnsureSuccessStatusCodeAsync();
return FooInfo.Content!;
}
1 answer
Refit allows such configuration out of the box bt using AddRefitClient
(which resembles the well-known AddHttpClient). This indeed requires explicit configuration for the HttpClient, but the configuration and usage will be simpler:
Refit and resilience configuration
private static IServiceCollection ConfigureResilience(this IServiceCollection services)
{
services
.AddRefitClient(typeof(IBarIntegration), (sp) =>
{
var accessTokenHelperService = sp.GetRequiredService<IAccessTokenHelperService>();
return new RefitSettings
{
AuthorizationHeaderValueGetter = () => accessTokenHelperService.GetAccessToken(default)
};
})
.ConfigureHttpClient((sp, client) =>
{
var BarSettings = sp.GetRequiredService<IOptions<BarApiSettings>>();
string baseUrl = BarSettings.Value.BaseUrl;
client.BaseAddress = new Uri(baseUrl);
})
.AddTransientHttpErrorPolicy(policyBuilder => policyBuilder.WaitAndRetryAsync( Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(1), RetryPolicyMaxCount)
)
);
.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(1), RetryPolicyMaxCount)));
return services;
}
Refit interface
[Headers("Authorization: Bearer")]
public interface IBarIntegration
{
[Get("/api/ext/Foo/GetFooBriefInfo")]
Task<GetFooBriefInfoForFooDto> GetFooBriefInfo(GetFooBriefInfoForFooInputDto inputData);
}
0 comment threads