This package is an ASP.NET Core integration for Trybot and contains extensions for the IHttpClientBuilder interface to make HttpClient calls more resilient and fault tolerant.
From ASP.NET Core 2.1 you can configure your HttpClient
instances via an IHttpClientBuilder
offered by the AddHttpClient()
function on the IServiceCollection
. This package extends this builder interface for configuring transient fault handling around the calls initiated by those HttpClients.
-
Configure your
HttpClient
public void ConfigureServices(IServiceCollection services) { services.AddHttpClient<CustomController>() .AddTrybotPolicy(options => options .Timeout(timeoutOptions => timeoutOptions .After(TimeSpan.FromSeconds(10))) .Retry(retryOptions => retryOptions .WhenExceptionOccurs(exception => exception is HttpRequestException) .WhenResultIs(result => result.StatusCode != HttpStatusCode.Ok) .WaitBetweenAttempts((attempt, result, exception) => TimeSpan.FromSeconds(5)) .WithMaxAttemptCount(3))); }
-
Then you can take the configured
HttpClient
by dependency injection in your controller:public class CustomController : Controller { private readonly HttpClient client; public CustomController(HttpClient client) { this.client = client; } public async Task CustomAction() { return Ok(await client.GetStringAsync("/something")); } }