For the complete documentation index, see llms.txt. This page is also available as Markdown.

Migrating to Polly v8

How-to · Applies to Brighter V10 · Prerequisites: Supporting Retry and Circuit Breaker

Both tails of the Polly story in one place. Four steps take a V9 handler using [UsePolicy] to a V10 handler using [UseResiliencePipeline], and the deprecated Polly v7 attribute is kept here because it is what you are migrating from. Supporting Retry and Circuit Breaker is the current reference.

Polly v8 Migration Guide: V9 to V10

Step 1: Update NuGet Packages

<!-- Remove old Polly v7 -->
<PackageReference Include="Polly" Version="7.x.x" Remove="true" />

<!-- Add Polly v8 -->
<PackageReference Include="Polly" Version="8.0.0" />
<PackageReference Include="Polly.Extensions" Version="8.0.0" />

Step 2: Replace Policy Registry with Resilience Pipeline Registry

V9:

// ...
var policyRegistry = new DefaultPolicy();

var retryPolicy = Policy
    .Handle<Exception>()
    .WaitAndRetry(new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(3) });

policyRegistry.Add("MyRetryPolicy", retryPolicy);

V10:

Step 3: Replace Attributes in Handlers

V9:

V10: a handler method takes one [UseResiliencePipeline], so the retry and the timeout become one pipeline. Add the retry first, so that it wraps the timeout and each attempt is timed on its own, as the two V9 attributes did:

Step 4: Update CommandProcessor Configuration

❌ V9 — superseded

✅ V10 — current

Three names change in that chain, and one of them is the entry point. With() becomes StartNew(), and Policies() — which took a Polly v7 IPolicyRegistry<string> on its own — is now the optional second argument to Resilience(). There is no separate call to make: pass both registries to Resilience during migration, and drop the second argument once no [UsePolicy] attributes remain. If you have nothing to carry over, DefaultResilience() replaces the pair.

Build the registry with AddBrighterDefault(). Brighter requires a pipeline registered under CommandProcessor.OutboxProducer, and Resilience throws ConfigurationException at startup when the registry you hand it does not have one. AddBrighterDefault uses TryAddBuilder, so it adds what is missing and leaves your own pipelines alone.

The V10 chain is also longer than V9's. NoExternalBus(), NoInstrumentation() and RequestSchedulerFactory() are steps the fluent interface now requires — the compiler will tell you, because each returns the interface the next call is declared on.


Legacy: Using Polly v7 Policies (Deprecated)

⚠️ DEPRECATED: The following section documents the legacy Polly v7 UsePolicy attribute, which is deprecated in favor of UseResiliencePipeline. This is maintained for backward compatibility only.

Using Brighter's UsePolicy Attribute (Legacy)

By adding the UsePolicy attribute, you instruct the Command Processor to insert a handler (filter) into the pipeline that runs all later steps using that Polly policy.

To configure the Polly policy you use the PolicyRegistry to register the Polly Policy with a name. At runtime we look up that Policy by name.

Start from Brighter's DefaultPolicy, a PolicyRegistry that already holds the CommandProcessor.RETRYPOLICY and CommandProcessor.CIRCUITBREAKER policies Brighter requires. Hand Brighter an empty PolicyRegistry and it throws ConfigurationException at startup.

You can use multiple policies with a handler, instead of passing in a single policy identifier, you can pass in an array of policy identifiers:

So if in addition to the above policy we have:

then you can add them both to your handler as follows:

Where we have multiple policies they are evaluated left to right, so in this case "MyCircuitBreakerPolicy" wraps "MyExceptionPolicy".

When creating policies, refer to the Polly documentation.

Whilst Polly does not support a Policy that is both Circuit Breaker and Retry i.e. retry n times with an interval between each retry, and then break circuit, to implement that put the Circuit Breaker first in the array, as above, so that it wraps the Retry. If retries expire, the exception bubbles out to the Circuit Breaker. A handler method takes one [UsePolicy] attribute, so the array is the only way to combine them.

Timeout (Legacy - Deprecated)

⚠️ DEPRECATED: The TimeoutPolicy attribute is obsolete in V10 and will be removed in V11. Use UseResiliencePipeline with Polly's Timeout strategy instead.

You should not allow a handler that calls out to another process (e.g. a call to a Database, queue, or an API) to run without a timeout. If the process has failed, you will consume a resource in your application polling that resource. This can cause your application to fail because another process failed.

Usually the client library you are using will have a timeout value that you can set.

In some scenarios the client library does not provide a timeout, so you have no way to abort.

We provide the Timeout attribute for that circumstance. You can apply it to a Handler to force that Handler into a thread which we will timeout, if it does not complete within the required time period.

V10 Replacement:


Further Reading

Last updated

Was this helpful?