Polly.Extensions.Http is an extensions package containing opinionated convenience methods for configuring Polly policies to handle transient faults typical of calls through HttpClient.
Перейти к файлу
Joel Hulen 93b91c4359
Update README.md
2023-11-13 15:46:00 -05:00
src v3.0.0: Reference Polly v7.1.0; rationalise solution layout; modernise build; add SourceLink. 2019-03-31 16:06:11 +01:00
.gitattributes Add gitignore and gitattributes 2018-03-26 18:02:41 +01:00
.gitignore Add gitignore and gitattributes 2018-03-26 18:02:41 +01:00
CHANGELOG.md v3.0.0: Reference Polly v7.1.0; rationalise solution layout; modernise build; add SourceLink. 2019-03-31 16:06:11 +01:00
CODE_OF_CONDUCT.md Add standard community-focused files 2018-03-26 18:05:00 +01:00
CONTRIBUTING.md Add standard community-focused files 2018-03-26 18:05:00 +01:00
GitVersionConfig.yaml v3.0.0: Reference Polly v7.1.0; rationalise solution layout; modernise build; add SourceLink. 2019-03-31 16:06:11 +01:00
LICENSE.txt Add BSD 3-clause license 2018-03-26 18:03:41 +01:00
Polly-Logo.jpg Add Polly logos 2018-03-26 18:05:55 +01:00
Polly-Logo.png Add Polly logos 2018-03-26 18:05:55 +01:00
Polly.png Add Polly logos 2018-03-26 18:05:55 +01:00
README.md Update README.md 2023-11-13 15:46:00 -05:00
appveyor.yml v3.0.0: Reference Polly v7.1.0; rationalise solution layout; modernise build; add SourceLink. 2019-03-31 16:06:11 +01:00
build.bat Add cake build 2018-03-26 20:59:36 +01:00
build.cake v3.0.0: Reference Polly v7.1.0; rationalise solution layout; modernise build; add SourceLink. 2019-03-31 16:06:11 +01:00
build.ps1 Add cake build 2018-03-26 20:59:36 +01:00

README.md

Polly.Extensions.Http

[!IMPORTANT] The Microsoft.Extensions.Http.Polly is deprecated in favor of the new Microsoft.Extensions.Http.Resilience package, as detailed here. This new package is built on top of Polly, and it takes full advantage of the new Polly v8 features.

Polly.Extensions.Http is an extensions package containing opinionated convenience methods for configuring Polly policies to handle transient faults typical of calls through HttpClient.

Polly.Extensions.Http targets .NET Standard 1.1 and .NET Standard 2.0.

Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.

Polly is a member of the .NET Foundation!

NuGet version Build status Slack Status

Installing via NuGet

Install-Package Polly.Extensions.Http

This package contains a strongly-named DLL.

Convenience methods for transient faults of HttpClient calls

This repo offers opinionated convenience methods for defining Polly policies to handle transient faults which may be raised by calls through HttpClient.

using Polly.Extensions.Http;

// ..

// Handles HttpRequestException, Http status codes >= 500 (server errors) and status code 408 (request timeout)
var policy = HttpPolicyExtensions
  .HandleTransientHttpError()
  .RetryAsync(3); // A very simple retry-3-times. See https://github.com/App-vNext/Polly for the many richer Policy configuration options available.

The pre-defined set of conditions to handle can be freely extended using Polly's usual Handle and Or clauses:

// Pre-canned http fault handling plus extra http status codes
var policy = HttpPolicyExtensions
  .HandleTransientHttpError()
  .OrResult(response => (int)response.StatusCode == someStatusCode) 
  .RetryAsync(3);

// Pre-canned http fault handling plus extra exceptions
var policy = HttpPolicyExtensions
  .HandleTransientHttpError()
  .Or<TimeoutRejectedException>() // TimeoutRejectedException from Polly's TimeoutPolicy
  .RetryAsync(3);

Related overloads in Polly's 'Or' syntax are also provided:

// Handles SomeException, HttpRequestException, Http status codes >= 500 (server errors) and status code 408 (request timeout)
// ('or' syntax after your own custom 'handle' clause)
var policy = Policy
  .Handle<SomeException>()
  .OrTransientHttpError()
  .RetryAsync(3);

// Handles SomeException, Http status codes >= 500 (server errors) and status code 408 (request timeout)
// ('or' syntax after your own custom 'handle' clause)
var policy = Policy
  .Handle<SomeException>()
  .OrTransientHttpStatusCode()
  .RetryAsync(3);

Using Polly.Extensions.Http with IHttpClientFactory

Polly.Extensions.Http is ideal for creating custom Polly policies for use with IHttpClientFactory, available from ASP.NET Core 2.1.

var retryPolicy = HttpPolicyExtensions
  .HandleTransientHttpError()
  .Or<TimeoutRejectedException>() // thrown by Polly's TimeoutPolicy if the inner execution times out
  .RetryAsync(3);

var timeoutPolicy = Policy.TimeoutAsync<HttpResponseMessage>(10);  

serviceCollection.AddHttpClient("example.com", c => c.BaseAddress = new Uri("http://example.com"))
  .AddPolicyHandler(retryPolicy)
  .AddPolicyHandler(timeoutPolicy);

Official documentation

Release notes

For details of changes by release see the change log.

3rd Party Libraries and Contributions

Acknowledgements

Instructions for Contributing

Since Polly is part of the .NET Foundation, we ask our contributors to abide by their Code of Conduct. To contribute (beyond trivial typo corrections), review and sign the .Net Foundation Contributor License Agreement. This ensures the community is free to use your contributions. The registration process can be completed entirely online.

Also, we've stood up a Slack channel for easier real-time discussion of ideas and the general direction of Polly as a whole.

License

Licensed under the terms of the New BSD License