Http.Resilience.Default 1.0.0

dotnet add package Http.Resilience.Default --version 1.0.0                
NuGet\Install-Package Http.Resilience.Default -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Http.Resilience.Default" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Http.Resilience.Default --version 1.0.0                
#r "nuget: Http.Resilience.Default, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Http.Resilience.Default as a Cake Addin
#addin nuget:?package=Http.Resilience.Default&version=1.0.0

// Install Http.Resilience.Default as a Cake Tool
#tool nuget:?package=Http.Resilience.Default&version=1.0.0                

Http Resilience Default NuGET Package

.NET

Resilience Pipelines

This package provides a resilience pipeline for HttpClient, with sensible defaults. The pipeline created can be used as is or configured further.

By default, pipelines are created with the following policies:

  • HTTP requests will be retried if the following exceptions are thrown:
    • TaskCanceledException
    • TimeoutRejectedException
  • HTTP requests will be retried if the response status code is any that don't fall within the 200-299 or 400-499 ranges.
  • Requests will be timed out after 30 seconds.
  • Requests will be retried twice (three total times).
  • Requests will be delayed by two seconds between retries.
  • Retries will be exponentially backed off.
  • Retries will utilize jitters, which will slightly randomize the delay between retries.

To set up a resilience pipeline, use the following code:

var pipelineBuilder = HttpResilience.GetResiliencePipelineBuilder();
var pipeline = pipelineBuilder.Build();

Retry configuration or timeouts can be configured:

// Default options.
var pipeLineBuilder = HttpResilience.GetResiliencePipelineBuilder();

// Pipeline key specified
var pipelineBuilder = HttpResilience.GetResiliencePipelineBuilder("my_resilience_pipeline");

// Retry configured.
var pipelineBuilder = HttpResilience.GetResiliencePipelineBuilder(x =>
{
    x.Delay = TimeSpan.FromSeconds(5);
    x.BackoffType = BackoffType.Linear;
});

// Timeout configured.
var pipelineBuilder = HttpResilience.GetResiliencePipelineBuilder(TimeSpan.FromSeconds(10));

// Retry and timeout configured.
var pipelineBuilder = HttpResilience.GetResiliencePipelineBuilder(x =>
{
    x.Delay = TimeSpan.FromSeconds(5);
    x.BackoffType = BackoffType.Linear;
}, TimeSpan.FromSeconds(10));

// Key, retry, and timeout configured.
var pipelineBuilder = HttpResilience.GetResiliencePipelineBuilder("my_resilience_pipeline", x =>
{
    x.Delay = TimeSpan.FromSeconds(5);
    x.BackoffType = BackoffType.Linear;
}, TimeSpan.FromSeconds(10));

When using these methods, you're expected to manage the pipeline and HttpClients yourself. This package also provides extensions for IServiceCollection and IHttpClientBuilder to make it easier to set up and use resilience pipelines.

Unless specified, the pipeline will use the key default_resilience_pipeline.

IServiceCollection Extensions

This package provides a AddHttpResiliencePipeline extension method for IServiceCollection that will add a resilience pipeline to the service collection. This sets up the pipeline similar to the example above, but allows you to dependency inject the pipeline into other services.

This method can be used as follows:

// Default options.
services.AddHttpResiliencePipeline();

// Pipeline key specified
services.AddHttpResiliencePipeline("my_resilience_pipeline");

// Retry configured.
services.AddHttpResiliencePipeline(x =>
{
    x.Delay = TimeSpan.FromSeconds(5);
    x.BackoffType = BackoffType.Linear;
});

// Timeout configured.
services.AddHttpResiliencePipeline(TimeSpan.FromSeconds(10));

// Retry and timeout configured.
services.AddHttpResiliencePipeline(x =>
{
    x.Delay = TimeSpan.FromSeconds(5);
    x.BackoffType = BackoffType.Linear;
}, TimeSpan.FromSeconds(10));

// Key, retry, and timeout configured.
services.AddHttpResiliencePipeline("my_resilience_pipeline", x =>
{
    x.Delay = TimeSpan.FromSeconds(5);
    x.BackoffType = BackoffType.Linear;
}, TimeSpan.FromSeconds(10));

Unless specified, the pipeline will use the key default_resilience_pipeline. This key can be used to retrieve the pipeline from the service collection.

Consuming the pipeline in a service can be done as follows:

ResiliencePipeline pipelineProvider = serviceProvider.GetRequiredService<ResiliencePipeline<string>>();
var pipeline = provider.GetPipeline(HttpResilienceConstants.DefaultPipelineKey);

IHttpClientBuilder Extensions

This package provides a AddHttpResilienceHandler extension method for IHttpClientBuilder that will add a resilience pipeline to the service collection. This sets up the pipeline similar to the example above, but allows you to dependency inject the pipeline into other services.

This method can be used as follows:

const string clientName = "my_client";

// Default options.
services.AddHttpClient(ClientKey)
  .AddHttpResilienceHandler();

// Pipeline key specified
services.AddHttpClient(ClientKey)
  .AddHttpResilienceHandler("my_resilience_pipeline");

// Retry configured.
services.AddHttpClient(ClientKey)
  .AddHttpResilienceHandler(x =>
{
    x.Delay = TimeSpan.FromSeconds(5);
    x.BackoffType = BackoffType.Linear;
});

// Timeout configured.
services.AddHttpClient(ClientKey)
  .AddHttpResilienceHandler(TimeSpan.FromSeconds(10));

// Retry and timeout configured.
services.AddHttpClient(ClientKey)
  .AddHttpResilienceHandler(x =>
{
    x.Delay = TimeSpan.FromSeconds(5);
    x.BackoffType = BackoffType.Linear;
}, TimeSpan.FromSeconds(10));

// Key, retry, and timeout configured.
services.AddHttpClient(ClientKey)
  .AddHttpResilienceHandler("my_resilience_pipeline", x =>
{
    x.Delay = TimeSpan.FromSeconds(5);
    x.BackoffType = BackoffType.Linear;
}, TimeSpan.FromSeconds(10));

Unless specified, the pipeline will use the key default_resilience_pipeline. This key is used internally within the HttpClient.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 92 6/17/2024