Skip to content

z4kn4fein/trybot-extensions-http

Folders and files

NameName
Last commit message
Last commit date

Latest commit

b28dcc0 · Jul 9, 2020

History

6 Commits
Oct 27, 2018
Oct 27, 2018
Oct 27, 2018
Oct 27, 2018
Oct 27, 2018
Jul 9, 2020
Oct 27, 2018
Oct 27, 2018
Oct 27, 2018
Oct 27, 2018

Repository files navigation

trybot-extensions-http Appveyor build status Travis CI build status NuGet Version

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.

Usage

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.

  1. 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)));
    }
  2. 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"));
        }
    }

Documentation

About

ASP.NET Core HttpClientBuilder extensions for Trybot (https://github.com/z4kn4fein/trybot)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published