You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
about: You have something specific to achieve and the existing documentation hasn't
4
-
covered how.
2
+
name: How to?
3
+
about: You have something specific to achieve and the existing documentation hasn't covered how.
5
4
---
6
5
7
-
<!--
8
-
Be sure to check out the ReadMe (https://github.com/App-vNext/Polly/blob/main/README.md) and wiki (https://github.com/App-vNext/Polly/wiki) first!
6
+
<!--
9
7
10
-
https://github.com/App-vNext/Polly/wiki contains detailed pages on each policy type. Many questions are answered there about policy operation, how to change operation with configuration options, and how to attach behaviour via delegates. Other wiki pages cover [unit-testing](https://github.com/App-vNext/Polly/wiki/Unit-testing-with-Polly), [async usage](https://github.com/App-vNext/Polly/wiki/Asynchronous-action-execution), common patterns, and [using HttpClientFactory in ASP.NET Core 2.1 as the best way to integrate Polly into outbound `HttpClient` calls](https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory).
8
+
Be sure to check out the README (https://github.com/App-vNext/Polly/blob/main/README.md) and documentation (https://www.pollydocs.org/) first!
11
9
12
10
-->
13
11
14
-
**Summary: What are you wanting to achieve?**
12
+
# How to?
15
13
16
-
**What code or approach do you have so far?**
14
+
## What are you wanting to achieve?
17
15
18
-
It is _always_ useful to see:
16
+
## What code or approach do you have so far?
19
17
20
-
+**code of any policy declarations**,
21
-
+**code of the calls to `.Execute/AndCapture/Async(...)`**
18
+
<!--
19
+
20
+
Best is a [complete runnable example](https://stackoverflow.com/help/mcve)
Copy file name to clipboardExpand all lines: .github/pull_request_template.md
+6-4Lines changed: 6 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,14 @@
1
-
<!-- Thank you for contributing to Polly! Open source is only as strong as its contributors. All non-trivial contributions get a public credit in the readme! -->
1
+
<!-- Thank you for contributing to Polly! Open source is only as strong as its contributors. -->
2
2
3
-
### The issue or feature being addressed
3
+
# Pull Request
4
+
5
+
## The issue or feature being addressed
4
6
5
7
<!-- Please include the existing GitHub issue number where relevant -->
6
8
7
-
###Details on the issue fix or feature implementation
9
+
## Details on the issue fix or feature implementation
8
10
9
-
###Confirm the following
11
+
## Confirm the following
10
12
11
13
-[ ] I started this PR by branching from the head of the default branch
12
14
-[ ] I have targeted the PR to merge into the default branch
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,5 @@
1
+
# CHANGELOG
2
+
1
3
## 8.0.0
2
4
3
5
* Updates for beta.2 by [@martincostello](https://github.com/martincostello) in https://github.com/App-vNext/Polly/pull/1580
Polly is part of the [.NET Foundation](https://dotnetfoundation.org/), and we ask our contributors to abide by their [Code of Conduct](https://www.dotnetfoundation.org/code-of-conduct).
1
+
# Code of Conduct
2
+
3
+
Polly is part of the [.NET Foundation][dnf], and we ask our contributors to abide
Copy file name to clipboardExpand all lines: docs/advanced/dependency-injection.md
+48-20Lines changed: 48 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,22 @@
1
1
# Dependency injection
2
2
3
-
Starting with version 8, Polly provides features that make the integration of Polly with the .NET [`IServiceCollection`](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection) Dependency Injection (DI) container more streamlined. This is a thin layer atop the [resilience pipeline registry](../pipelines/resilience-pipeline-registry.md) which manages resilience pipelines.
3
+
Starting with version 8, Polly provides features that make the integration of Polly
4
+
with the .NET [`IServiceCollection`](https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection)
5
+
Dependency Injection (DI) container more streamlined. This is a thin layer atop the
6
+
[resilience pipeline registry](../pipelines/resilience-pipeline-registry.md) which
7
+
manages resilience pipelines.
4
8
5
9
## Usage
6
10
7
-
To use the DI functionality, add the [`Polly.Extensions`](https://www.nuget.org/packages/Polly.Extensions) package to your project:
11
+
To use the DI functionality, add the [`Polly.Extensions`](https://www.nuget.org/packages/Polly.Extensions)
12
+
package to your project:
8
13
9
14
```sh
10
15
dotnet add package Polly.Extensions
11
16
```
12
17
13
-
Afterwards, you can use the `AddResiliencePipeline(...)` extension method to set up your pipeline:
18
+
Afterwards, you can use the `AddResiliencePipeline(...)` extension method to set
19
+
up your pipeline:
14
20
15
21
<!-- snippet: add-resilience-pipeline -->
16
22
```cs
@@ -44,20 +50,24 @@ await pipeline.ExecuteAsync(
44
50
```
45
51
<!-- endSnippet -->
46
52
47
-
The `AddResiliencePipeline` extension method also registers the following services into the DI:
53
+
The `AddResiliencePipeline` extension method also registers the following services
54
+
into the DI container:
48
55
49
56
-`ResiliencePipelineRegistry<string>`: Allows adding and retrieving resilience pipelines.
-`IOptions<ResiliencePipelineRegistryOptions<string>>`: Options for `ResiliencePipelineRegistry<string>`.
52
59
53
60
> [!NOTE]
54
-
> The generic `string`` is inferred since the pipeline was defined using the "my-key" value.
61
+
> The generic `string`` is inferred since the pipeline was defined using the
62
+
> "my-key" value.
55
63
56
-
If you only need the registry without defining a pipeline, use the `AddResiliencePipelineRegistry(...)` method.
64
+
If you only need the registry without defining a pipeline, use the
65
+
`AddResiliencePipelineRegistry(...)` method.
57
66
58
67
### Generic resilience pipelines
59
68
60
-
You can also define generic resilience pipelines (`ResiliencePipeline<T>`), as demonstrated below:
69
+
You can also define generic resilience pipelines (`ResiliencePipeline<T>`), as
70
+
demonstrated below:
61
71
62
72
<!-- snippet: add-resilience-pipeline-generic -->
63
73
```cs
@@ -92,7 +102,9 @@ await pipeline.ExecuteAsync(
92
102
93
103
## Dynamic reloads
94
104
95
-
Dynamic reloading is a feature of the pipeline registry that is also surfaced when using the `AddResiliencePipeline(...)` extension method. Use an overload that provides access to `AddResiliencePipelineContext`:
105
+
Dynamic reloading is a feature of the pipeline registry that is also surfaced when
106
+
using the `AddResiliencePipeline(...)` extension method. Use an overload that provides
107
+
access to `AddResiliencePipelineContext`:
96
108
97
109
<!-- snippet: di-dynamic-reloads -->
98
110
```cs
@@ -121,11 +133,14 @@ During a reload:
121
133
- The callback re-executes.
122
134
- The previous pipeline is discarded.
123
135
124
-
If an error occurs during reloading, the old pipeline remains, and dynamic reloading stops.
136
+
If an error occurs during reloading, the old pipeline remains, and dynamic
137
+
reloading stops.
125
138
126
139
## Resource disposal
127
140
128
-
Like dynamic reloading, the pipeline registry's resource disposal feature lets you register callbacks. These callbacks run when the pipeline is discarded, reloaded, or the registry is disposed at application shutdown.
141
+
Like dynamic reloading, the pipeline registry's resource disposal feature lets
142
+
you register callbacks. These callbacks run when the pipeline is discarded, reloaded,
143
+
or the registry is disposed at application shutdown.
This feature ensures that resources are properly disposed when a pipeline reloads, discarding the old version.
163
+
This feature ensures that resources are properly disposed when a pipeline
164
+
reloads, discarding the old version.
149
165
150
166
## Complex pipeline keys
151
167
152
-
The `AddResiliencePipeline(...)` method supports complex pipeline keys. This capability allows you to define the structure of your pipeline and dynamically resolve and cache multiple instances of the pipeline with different keys.
168
+
The `AddResiliencePipeline(...)` method supports complex pipeline keys. This
169
+
capability allows you to define the structure of your pipeline and dynamically
170
+
resolve and cache multiple instances of the pipeline with different keys.
153
171
154
172
Start by defining your complex key:
155
173
@@ -174,7 +192,10 @@ services.AddResiliencePipeline(new MyPipelineKey("my-pipeline", string.Empty), b
174
192
```
175
193
<!-- endSnippet -->
176
194
177
-
The "my-pipeline" pipeline is now registered. Note that the `InstanceName` is an empty string. While we're registering the builder action for a specific pipeline, the `InstanceName` parameter isn't used during the pipeline's registration. Some further modifications are required for this to function.
195
+
The "my-pipeline" pipeline is now registered. Note that the `InstanceName` is an
196
+
empty string. While we're registering the builder action for a specific pipeline,
197
+
the `InstanceName` parameter isn't used during the pipeline's registration. Some
198
+
further modifications are required for this to function.
178
199
179
200
Introduce the `PipelineNameComparer`:
180
201
@@ -207,11 +228,16 @@ services
207
228
208
229
Let's summarize our actions:
209
230
210
-
- We assigned the `PipelineNameComparer` instance to the `BuilderComparer` property. This action changes the default registry behavior, ensuring that only the `PipelineName` is used to find the associated builder.
211
-
- We used the `InstanceNameFormatter` delegate to represent the `MyPipelineKey` as an instance name for telemetry purposes, keeping the instance name as it is.
212
-
- Likewise, the `BuilderNameFormatter` delegate represents the `MyPipelineKey` as a builder name in telemetry.
231
+
- We assigned the `PipelineNameComparer` instance to the `BuilderComparer` property.
232
+
This action changes the default registry behavior, ensuring that only the
233
+
`PipelineName` is used to find the associated builder.
234
+
- We used the `InstanceNameFormatter` delegate to represent the `MyPipelineKey`
235
+
as an instance name for telemetry purposes, keeping the instance name as it is.
236
+
- Likewise, the `BuilderNameFormatter` delegate represents the `MyPipelineKey` as
237
+
a builder name in telemetry.
213
238
214
-
Finally, use the `ResiliencePipelineProvider<MyPipelineKey>` to dynamically create and cache multiple instances of the same pipeline:
239
+
Finally, use the `ResiliencePipelineProvider<MyPipelineKey>` to dynamically create
240
+
and cache multiple instances of the same pipeline:
Over the years, many developers have used Polly in various ways. Some of these recurring patterns may not be ideal. This section highlights the recommended practices and those to avoid.
256
+
Over the years, many developers have used Polly in various ways. Some of these
257
+
recurring patterns may not be ideal. This section highlights the recommended practices
258
+
and those to avoid.
232
259
233
260
### 1 - Accessing the `IServiceCollection` instead of `IServiceProvider`
0 commit comments