-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
153 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Polly; | ||
using Polly.Registry; | ||
using Polly.Retry; | ||
|
||
namespace Snippets.Readme; | ||
|
||
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously | ||
|
||
internal static class Snippets | ||
{ | ||
public static async Task QuickStart() | ||
{ | ||
#region quick-start | ||
|
||
// Create a instance of builder that exposes various extensions for adding resilience strategies | ||
var builder = new ResiliencePipelineBuilder(); | ||
|
||
// Add retry using the default options: | ||
// - 3 retry attempts | ||
// - 1-second delay between retries | ||
// - Handles all exceptions except OperationCanceledException | ||
builder.AddRetry(new RetryStrategyOptions()); | ||
|
||
// Add 10 second timeout | ||
builder.AddTimeout(TimeSpan.FromSeconds(10)); | ||
|
||
// Build the resilience pipeline | ||
ResiliencePipeline pipeline = builder.Build(); | ||
|
||
// Execute the pipeline | ||
await pipeline.ExecuteAsync(async token => | ||
{ | ||
// Your custom logic here | ||
}); | ||
|
||
#endregion | ||
} | ||
|
||
public static async Task QuickStartDi() | ||
{ | ||
#region quick-start-di | ||
|
||
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 | ||
IServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
|
||
// Retrieve ResiliencePipelineProvider that caches and dynamically creates the resilience pipelines | ||
var pipelineProvider = serviceProvider.GetRequiredService<ResiliencePipelineProvider<string>>(); | ||
|
||
// Retrieve resilience pipeline by the string-based name | ||
ResiliencePipeline pipeline = pipelineProvider.GetPipeline("my-pipeline"); | ||
|
||
// Execute the pipeline | ||
await pipeline.ExecuteAsync(async token => | ||
{ | ||
// Your custom logic here | ||
}); | ||
|
||
#endregion | ||
} | ||
} |