I'm try setting up a IHttpClientFactory
, and i'd like to know how to send it parameters when it is created, those parameters i need to assign to retry policy.
I'm using .Net Core 2.2 and Microsoft.Extensions.Http.Polly, I've read this post
I have this is Startup.cs
services.AddHttpClient("MyClient", c =>
{
c.BaseAddress = new Uri("http://interface.net");
c.DefaultRequestHeaders.Add("Accept", "application/json");
})
.AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(600)));
I used it in this way
private readonly IHttpClientFactory _iHttpClientFactory;
public ValuesController(IHttpClientFactory iHttpClientFactory)
{
_iHttpClientFactory = iHttpClientFactory;
}
public async Task<ActionResult<string>> Get()
{
var client = _iHttpClientFactory.CreateClient("MyClient");
var response = await client.GetAsync("/Service?Id=123");
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsStringAsync();
return result;
}
I'd like to know if there is a way to send parameters when i execute the CreateClient
, for assign to retryCount
and sleepDuration
in the AddTransientHttpErrorPolicy
, in this case 3 and 600 respectively, because i need to create clients with different retryCounts
and sleepDurations
and those values can change.
Something like this
var retryCount = 5;
var sleepDuration = 400;
var client = _iHttpClientFactory.CreateClient("MyClient", retryCount, sleepDuration);
Or another way?