You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I register HttpClient via IServiceCollection.AddHttpClient<T>(). However, it seems there is no way to create an HttpClient for T. Because WebApplicationFactory has only CreateClient(); method.
publicvoidConfigureServices(IServiceCollectionservices){
...services.SetWaitAndRetryPolicy<CustomHttpClient>();}publicstaticclassIServiceCollectionExtension{publicstaticvoidSetWaitAndRetryPolicy<T>(thisIServiceCollectionservices)whereT:class{//https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests//https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory Randomrandom=newRandom();Configconfig=null;services.AddHttpClient<T>((sp,client)=>config=sp.GetService<IOptions<Config>>().Value).AddPolicyHandler((service,request)=>//TransientErrors://Network failures(System.Net.Http.HttpRequestException)//HTTP 5XX status codes(server errors)//HTTP 408 status code(request timeout)HttpPolicyExtensions.HandleTransientHttpError()//Wait and retry with exponential backoff with Randomisation.WaitAndRetryAsync(config.Retry.RetryCount,
retryCount =>TimeSpan.FromSeconds(Math.Pow(2,retryCount))+//2,4,8,...TimeSpan.FromMilliseconds(random.Next(100,990)),//random in millisecsonRetry:(outcome,timespan,retryCount,context)=>{service.GetService<ILog>().Info("Delaying for {delay}ms, then making retry {retry}.",timespan.TotalMilliseconds,retryCount);})).AddPolicyHandler((service,request)=>HttpPolicyExtensions.HandleTransientHttpError()//Further external requests are blocked for 30 seconds if five failed attempts occur sequentially.//Circuit breaker policies are stateful.All calls through this client share the same circuit state..CircuitBreakerAsync(config.CircuitBreaker.HandledEventsAllowedBeforeBreaking,TimeSpan.FromSeconds(config.CircuitBreaker.DurationOfBreakInSeconds),(result,timeSpan,context)=>service.GetService<ILog>().Info("CircuitBreaker onBreak for {delay}ms",timeSpan.TotalMilliseconds),
context =>service.GetService<ILog>().Info("CircuitBreaker onReset")));}}publicclassHttpClientService{privatereadonlyHttpClient_client;privatereadonlyILog_logger;publicHttpClientService(HttpClientclient,ILoglogger){_client=client;_logger=logger;}publicasyncTask<HttpStatusCode>PostAsync(stringurl,Dictionary<string,string>headers,stringbody){using(varcontent=newStringContent(body,Encoding.UTF8,"application/json")){foreach(varkeyValueinheaders){content.Headers.Add(keyValue.Key,keyValue.Value);}varresponse=await_client.PostAsync(url,content);response.EnsureSuccessStatusCode();returnresponse.StatusCode;}}}
The text was updated successfully, but these errors were encountered:
Because WebApplicationFactory has only CreateClient(); method, it has not overloads that create a named or typed HttpClient. This allows me to assign Polly polcy to a named or typed HttpClient.
Because I need to test Polly policy showed on the OP.
Thank you for contacting us. Due to no activity on this issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.
Uh oh!
There was an error while loading. Please reload this page.
I register HttpClient via
IServiceCollection.AddHttpClient<T>()
. However, it seems there is no way to create an HttpClient for T. Because WebApplicationFactory has only CreateClient(); method.The text was updated successfully, but these errors were encountered: