Skip to content

Latest commit

 

History

History

Bet.Extensions.Resilience.Abstractions

Bet.Extensions.Resilience.Abstractions

GitHub license Build status NuGet Nuget feedz.io

The second letter in the Hebrew alphabet is the ב bet/beit. Its meaning is "house". In the ancient pictographic Hebrew it was a symbol resembling a tent on a landscape.

Note: Pre-release packages are distributed via feedz.io.

Summary

The abstraction library for Bet.Extensions.Resilience Polly.

It provides with easy configuration syntax for Polly policies.

buymeacoffee

Give a Star! ⭐

If you like or are using this project to learn or start your solution, please give it a star. Thanks!

Install

    dotnet add package Bet.Extensions.Resilience.Abstractions

Usage

  1. Add Policies with their Shapes
services.AddPollyPolicy<AsyncTimeoutPolicy, TimeoutPolicyOptions>("TimeoutPolicyOptimistic")

        .ConfigurePolicy(
            sectionName: PolicyOptionsKeys.TimeoutPolicy,
            (policy) =>
            {
                policy.CreateTimeoutAsync(TimeoutStrategy.Optimistic);
            })

        .ConfigurePolicy(
            sectionName: PolicyOptionsKeys.TimeoutPolicy,
            (policy) =>
            {
                policy.ConfigurePolicy = (options, logger) =>
                {
                    logger.LogInformation("Hello TimeoutPolicyOptimistic");

                    return Policy.TimeoutAsync(options.Timeout, TimeoutStrategy.Optimistic);
                };
            },
            policyName: "TimeoutPolicyAsync");

services.AddPollyPolicy<AsyncTimeoutPolicy<bool>, TimeoutPolicyOptions>("TimeoutPolicyPessimistic")
        .ConfigurePolicy(
            sectionName: "DefaultPolicy:TimeoutPolicy",
            (policy) =>
            {
                PolicyShapes.CreateTimeoutAsync<TimeoutPolicyOptions, bool>(policy);
            });
  1. Use the Polly policy withing the other components by getting values from the PolicyBucket registry.
    var policy = host.Services.GetRequiredService<PolicyBucket<AsyncTimeoutPolicy, TimeoutPolicyOptions>>();

    var optimisticPolicy = policy.GetPolicy("TimeoutPolicyAsync") as IAsyncPolicy;
    var pessimisticPolicy = policy.GetPolicy("TimeoutPolicyPessimistic") as IAsyncPolicy<bool>;
  1. Or using IPolicyRegistry<string> if the policies are configured for the host
    var policyRegistry = host.Services.GetRequiredService<IPolicyRegistry<string>>();

    var pessemisticPolicy = host.Services.GetRequiredService<IPolicyRegistry<string>>()
                    .Get<IAsyncPolicy<bool>>("TimeoutPolicyPessimistic");

Design

Policy can be:

  1. void Async
  2. void Sunc
  3. Async
  4. Sync

Policy can have Options:

  • Specific to the policy configuration i.e. MaxRetries
  • Configurations provider can raise change event that can be monitored. Upon the change registrations for policies must be updated.

Policy can be registered with:

  • IPolicyRegistry<string>
  • Dependency Injection

Policy can combine other policies

  • WrapAsync
  • Wrap