Fences
Fences is a .NET resilience and transient-fault-handling library that allows developers to express resilience strategies such as Retry, Circuit Breaker, Hedging, Timeout, Rate Limiter and Fallback in a fluent and thread-safe manner.
Fences is a community fork of Polly, maintained by Brighter Command. It is not affiliated with, endorsed by, or supported by App vNext or the Polly maintainers. The fork was taken from Polly 8.7.0 to avoid the Open Source Maintainers Fee now charged for Polly; see ADR 0002 for the reasoning and NOTICE.md for provenance and attribution.
Why Fences exists
Polly's source is BSD 3-Clause licensed, and it stays open source. That license lets anyone redistribute it, in source or binary form, provided the copyright notice and disclaimer are retained - which NOTICE.md and LICENSE do. Building and publishing our own binaries from that source is squarely within the grant.
The Open Source Maintainers Fee (OSMF) is App vNext's policy for the binaries they publish: a commercial consumer of those binaries may owe a fee above a revenue threshold. It governs their distribution. It does not change the source license, and it does not reach binaries built by anyone else.
That freedom is the OSMF's own argument. The case for charging for binaries is that it stays within open source precisely because the source remains open and anyone is free to build and publish their own binaries. That is what separates the model from proprietary licensing. But it is one door, and it opens both ways: the principle that makes the fee compatible with open source is the same principle that makes Fences legitimate. We are not going around the OSMF - we are walking through the door it holds open.
Fences is the binary somebody else builds. We compile from the BSD 3-Clause source, publish under our own branding, and we do not apply the OSMF. Fences binaries are free for everyone - no revenue threshold, no fee. If you use Polly and would rather not pay the fee, Fences is a drop-in alternative: change your package references and your namespaces, as described below.
We track the Polly API today and expect to keep doing so, and we may adopt upstream changes. But Fences is under Brighter Command's stewardship now and will evolve to suit its users, so a future major version may diverge from Polly and stop being API-compatible. We will say so plainly when it does; it will not arrive in a patch.
This is not a criticism of App vNext contributors, who have maintained Polly for years. Publishing our own binaries is exactly what the license they chose invites us to do, and we have responded to that by doing so. See ADR 0002 for the full reasoning.
This README describes the v8 API. The pre-v8 API is still shipped in the Paramore.Fences package; see the v7 documentation.
NuGet packages
All five are published as stable releases starting with 9.0.0.
| Package |
Replaces |
About |
Paramore.Fences.Core |
Polly.Core |
The core abstractions and built-in strategies. |
Paramore.Fences.Extensions |
Polly.Extensions |
Dependency injection and telemetry. |
Paramore.Fences.RateLimiting |
Polly.RateLimiting |
Integration with the System.Threading.RateLimiting APIs. |
Paramore.Fences.Testing |
Polly.Testing |
Testing support. |
Paramore.Fences |
Polly |
The legacy API exposed by versions before version 8. |
The library, the repository and the documentation site are all called Fences. Only the package and assembly identifiers carry the Paramore prefix, which is the identifier family the rest of the Brighter Command projects use.
Version numbers
Fences version numbers are our own and do not line up with Polly's. Fences started at 9.0.0 because moving from Polly to Paramore.Fences is a breaking change for every consumer, and a new major version is the honest signal for that (ADR 0002, D3). It does not mean Fences is based on a "Polly 9".
- Fences 9.x is the Polly 8 API. It is Polly 8.7.0 plus the upstream changes we have adopted since, rebranded. A Fences 9.x minor or patch release may carry fixes that Polly has not released yet, but it does not break the API.
- Fences 9 is not Polly 9. If Polly ships a 9.0, it is a separate release with its own changes. Fences does not follow it automatically, and the numbers matching means nothing. We review upstream changes one at a time (ADR 0003); one that would break the API waits for a Fences major version, and we will say so plainly when that happens.
Documentation
This README aims to give a quick overview of some Fences features - including enough to get you started with any resilience strategy. For more detail on any resilience strategy, and many other aspects of Fences, see the documentation, which is also published at brightercommand.github.io/Fences.
Migrating from Polly
For a project on Polly 8.7.0, moving to Fences is a change of package reference and namespace, not a rewrite:
-<PackageReference Include="Polly.Core" Version="8.7.0" />
+<PackageReference Include="Paramore.Fences.Core" Version="9.0.0" />
-using Polly;
+using Paramore.Fences;
Every namespace maps one-for-one, and exactly one public type was renamed. The one thing a find-and-replace over your own source will not fix is the telemetry names, which changed from Polly and resilience.polly.* to Paramore.Fences and resilience.fences.*. See Migrate from Polly for the full mapping, and for the one case Fences cannot help with - Microsoft.Extensions.Http.Resilience keeps Polly in your dependency graph regardless.
Quick start
To use Fences, you must provide a callback and execute it using a resilience pipeline. A resilience pipeline combines one or more resilience strategies, such as retry, timeout, and rate limiter. Fences uses builders to integrate these strategies into a pipeline.
To get started, first add the Paramore.Fences.Core package to your project by running the following command:
dotnet add package Paramore.Fences.Core
You can create a ResiliencePipeline using the ResiliencePipelineBuilder class as shown below:
// Create an instance of builder that exposes various extensions for adding resilience strategies
ResiliencePipeline pipeline = new ResiliencePipelineBuilder()
.AddRetry(new RetryStrategyOptions()) // Add retry using the default options
.AddTimeout(TimeSpan.FromSeconds(10)) // Add 10 seconds timeout
.Build(); // Builds the resilience pipeline
// Execute the pipeline asynchronously
await pipeline.ExecuteAsync(static async token => { /* Your custom logic goes here */ }, cancellationToken);
Dependency injection
If you prefer to define resilience pipelines using IServiceCollection, you'll need to install the Paramore.Fences.Extensions package:
dotnet add package Paramore.Fences.Extensions
You can then define your resilience pipeline using the AddResiliencePipeline(...) extension method as shown:
var services = new ServiceCollection();
// Define a resilience pipeline with the name "my-pipeline"
services.AddResiliencePipeline("my-pipeline", builder =>
{
builder
.AddRetry(new RetryStrategyOptions())
.AddTimeout(TimeSpan.FromSeconds(10));
});
// Build the service provider
var serviceProvider = services.BuildServiceProvider();
// Retrieve a ResiliencePipelineProvider that dynamically creates and caches the resilience pipelines
var pipelineProvider = serviceProvider.GetRequiredService<ResiliencePipelineProvider<string>>();
// Retrieve your resilience pipeline using the name it was registered with
ResiliencePipeline pipeline = pipelineProvider.GetPipeline("my-pipeline");
// Alternatively, you can use keyed services to retrieve the resilience pipeline
pipeline = serviceProvider.GetRequiredKeyedService<ResiliencePipeline>("my-pipeline");
// Execute the pipeline
await pipeline.ExecuteAsync(static async token =>
{
// Your custom logic goes here
});
Resilience strategies
Fences provides a variety of resilience strategies. Alongside the comprehensive guides for each strategy, the documentation also includes an overview of the role each strategy plays in resilience engineering.
Fences categorizes resilience strategies into two main groups:
Reactive
These strategies handle specific exceptions thrown or results returned by callbacks executed through the strategy.
| Strategy |
Premise |
AKA |
Mitigation |