PolicyBuilder.NET 1.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package PolicyBuilder.NET --version 1.1.2                
NuGet\Install-Package PolicyBuilder.NET -Version 1.1.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PolicyBuilder.NET" Version="1.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PolicyBuilder.NET --version 1.1.2                
#r "nuget: PolicyBuilder.NET, 1.1.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PolicyBuilder.NET as a Cake Addin
#addin nuget:?package=PolicyBuilder.NET&version=1.1.2

// Install PolicyBuilder.NET as a Cake Tool
#tool nuget:?package=PolicyBuilder.NET&version=1.1.2                

PolicyBuilder.NET

Overview

PolicyBuilder.NET is a library designed to enhance the resilience of Refit clients by integrating Polly policies. This library allows you to configure Refit clients with retry, circuit breaker, and timeout policies, ensuring that your HTTP requests are more robust and fault-tolerant.

Features

  • Retry Policy: Automatically retries failed requests a specified number of times with a delay between each attempt.
  • Circuit Breaker Policy: Monitors the number of consecutive failures and opens the circuit if the threshold is reached, preventing further requests for a specified duration.
  • Timeout Policy: Enforces a maximum duration for each request, ensuring that long-running requests are terminated.

Installation

To install PolicyBuilder.NET, add the PolicyBuilder.NET NuGet package to your project:

dotnet add package PolicyBuilder.NET 

Configuration

appsettings.json

Configure your resiliency policies and Refit client settings in your appsettings.json file:

  "HttpBin": {
    "BaseUrl": "https://httpbin.org",
    "ResiliencyPolicies": {
            "RetryPolicy": {
        "Count": 3,
        "Delay": 2,
        "Jitter": 1000
      },
      "CircuitBreakerPolicy": {
        "Count": 5,
        "Duration": 5
      },
      "TimeoutPolicy": {
        "Duration": 10
      }
    }
  },

Example Usage

1. Define Your Refit Interface

Create an interface for your Refit client: Renders as:

public interface IMyRefitClient { [Get("/endpoint")] Task<ApiResponse> GetEndpointAsync(); }

How to use it

First step is to register the register the policies defined in the appsettings.json to the policy registry


    // Configure Polly policies
    var configuredPolicyName = services.ConfigurePollyPolicies<IMyRefitClient>(configuration, sectionName);

Once the policies are registered with the policy registry in the above method, we get the name of the policy as return value from the method. Use this to the http client builder(IHttpClientBuilder)


    // Configure the Refit client with the base URL and Polly policies
    services.AddRefitClient<IMyRefitClient>() // or any other means to //add your http client
        .AddPolicyHandlerFromRegistry(configuredPolicyName);
  • TClient: The type of the Refit client.
  • services: The service collection.
  • configuration: The configuration.
  • sectionName: The configuration section name.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any changes.

Contact

If you have any questions, suggestions, or would like to contribute to this project, feel free to reach out!

You can also open an issue on GitHub Issues if you encounter any problems or have feature requests.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0.10 103 9/29/2024
1.2.0.7 77 9/29/2024
1.1.3 86 9/29/2024
1.1.2 81 9/29/2024
1.1.1 83 9/29/2024
1.1.0 90 9/29/2024
1.0.6 79 9/28/2024
1.0.5 83 9/28/2024