-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
PolicyRegistry
available from v5.2 onwards
PolicyRegistry
provides a registry for storing configured policy instances and retrieving them later for use.
PolicyRegistry
promotes separation of policy definition and usage, enabling common patterns such as defining policies centrally on start-up, and accessing them at point-of-use as an injected dependency.
PolicyRegistry
has Dictionary<string, Policy>
-like semantics.
PolicyRegistry registry = new PolicyRegistry();
registry.Add("StandardHttpResilience", myStandardHttpResiliencePolicy);
// Or:
registry["StandardHttpResilience"] = myStandardHttpResiliencePolicy;
// Pass the registry instance to usage sites by DI, perhaps
public class MyServiceGateway
{
public void MyServiceGateway(..., IReadOnlyPolicyRegistry<string> registry, ...)
{
...
}
}
Other patterns are of course possible: for example, a PolicyRegistry
instance could be exposed as a thread-safe singleton, allowing it to be used as an ambient context.
For those who want to enforce separation of registry population (perhaps at startup) from registry usage, an IReadOnlyPolicyRegistry<in TKey>
interface exists, allowing you to pass a read-only registry to usage sites.
var policy = registry.Get<IAsyncPolicy<HttpResponseMessage>>("StandardHttpResilience");
HttpResponseMessage response = await policy.ExecuteAsync<HttpResponseMessage>(...);
// Or:
HttpResponseMessage response = await
registry.Get<IAsyncPolicy<HttpResponseMessage>>("StandardHttpResilience")
.ExecuteAsync<HttpResponseMessage>(...);
// Or:
HttpResponseMessage response = await
((IAsyncPolicy<HttpResponseMessage>)registry["StandardHttpResilience"])
.ExecuteAsync<HttpResponseMessage>(...);
PolicyRegistry
exposes further semantics, with standard dictionary-like behaviour:
bool ContainsKey(TKey key)
bool TryGet<TPolicy>(TKey key, out TPolicy policy)
int Count
void Clear()
-
bool Remove(TKey key)
.
The default implementation is in-memory, backed by ConcurrentDictionary
.
The default implementation is (intentionally) agnostic about what the string key
represents, for maximum flexibility. Users may define their own patterns for keys.
Polly also exposes an IPolicyRegistry<in TKey>
interface. You may fulfil this interface with your own implementation. This could be used, for example:
- to provide an implementation backed by an alternative store
- to provide an implementation with a compound
TKey
of some kind.
- Home
- Polly RoadMap
- Contributing
- Transient fault handling and proactive resilience engineering
- Supported targets
- Retry
- Circuit Breaker
- Advanced Circuit Breaker
- Timeout
- Bulkhead
- Cache
- Rate-Limit
- Fallback
- PolicyWrap
- NoOp
- PolicyRegistry
- Polly and HttpClientFactory
- Asynchronous action execution
- Handling InnerExceptions and AggregateExceptions
- Statefulness of policies
- Keys and Context Data
- Non generic and generic policies
- Polly and interfaces
- Some policy patterns
- Debugging with Polly in Visual Studio
- Unit-testing with Polly
- Polly concept and architecture
- Polly v6 breaking changes
- Polly v7 breaking changes
- DISCUSSION PROPOSAL- Polly eventing and metrics architecture