![]() Var client = _clientFactory.CreateClient("TheCodeBuzz") Public WeatherForecastController(ILogger logger, Private readonly IHttpClientFactory _clientFactory Private readonly IBusinessLayer _businessLayer Public class WeatherForecastController : ControllerBase In your Program.cs in the main method please add UseServiceProviderFactory extension and use the AutofacServiceProviderFactory. Register the Generic Host Builder to use Autofac Factory. Step 1 – Register HostBuilder to use Autofac Factory Use Nuget manager, Configure Autofac DI Container in ASPNET Core 50 Filters are supported in most ASP.NET Core project templates, Please choose any ASP.NET Core API project template. Let’s create an ASP.NET core API application.There are a lot of benefits of the DI approach like, Step 3 – Use HttpClientfactory using Dependency InjectionĬontainer controls the lifetime of services used and the consumer need not have to worry about disposing of them once after every use.Step 2 – Define Autofac DI Container with HttpClientfactory.Step 1 – Register HostBuilder to use Autofac Factory.Sponsor: Check out JetBrains Rider: a cross-platform. Thanks to Dylan Reisenberger for his help on this post, along with Joel Hulen! Also read more about HttpClientFactory on Steve Gordon's blog and learn more about HttpClientFactory and Polly on the Polly project site. HttpClient's have a Timeout which is "all tries overall timeout" while a TimeoutPolicy inside a Retry is "timeout per try." Again, be aware. You may want to have a GET policy and a post one and use different HttpClients. What would happen if you applied a Polly Retry Policy to an HttpClient and it POSTed twice? Is that backend behavior compatible with your policies? Know what the behavior you expect is and plan for it. "From a RESTful service standpoint, for an operation (or service call) to be idempotent, clients can make that same call repeatedly while producing the same result."īut everyone's API is different. If you are POSTing to an endpoint and applying retries, you want that operation to be idempotent. NET 4.5, or anything that's compliant with. Įven though it works great with ASP.NET Core 2.1 (best, IMHO) you can use Polly with. Go read up on Polly at and check out the extensive samples at. If those behaviors aren't actual Business Logic (tm) then why not get them out of your code? I can have as many named or typed HttpClients as I like and they can have all kinds of specific policies with VERY sophisticated behaviors. I like AddTransientHttpErrorPolicy because it automatically handles Http5xx's and Http408s as well as the occasional. Change it to this: services.AddHttpClient().ĪddTransientHttpErrorPolicy(policyBuilder => policyBuilder.CircuitBreakerAsync(Īnd now I've got CircuitBreaker where it backs off for a minute if it's broken (hit a handled fault) twice! To this (after adding "using Polly " as a namespace) services.AddHttpClient().ĪddTransientHttpErrorPolicy(policyBuilder => policyBuilder.RetryAsync(2)) Īnd now I've got Retries. I just went into my Startup.cs and changed this services.AddHttpClient() HttpClientFactory in ASPNET Core 2.1 provides a way to pre-configure instances of HttpClient which apply Polly policies to every outgoing call. ![]() Polly is an OSS library with a lovely package that you can use to combine the goodness of Polly with ASP.NET Core 2.1. I'd like a central way to manage HttpClient policy!Įnter Polly. However, I don't think I should have to do those things because while they are behaviors, they are really cross-cutting policies. OR, I could put it in a base class and build a series of HttpClient utilities. If I want features like Retry and Timeout, I could end up littering my code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |