PolicyBuilder.NET
1.1.1
See the version list below for details.
dotnet add package PolicyBuilder.NET --version 1.1.1
NuGet\Install-Package PolicyBuilder.NET -Version 1.1.1
<PackageReference Include="PolicyBuilder.NET" Version="1.1.1" />
paket add PolicyBuilder.NET --version 1.1.1
#r "nuget: PolicyBuilder.NET, 1.1.1"
// Install PolicyBuilder.NET as a Cake Addin #addin nuget:?package=PolicyBuilder.NET&version=1.1.1 // Install PolicyBuilder.NET as a Cake Tool #tool nuget:?package=PolicyBuilder.NET&version=1.1.1
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!
- Email: zameer.kh@gmail.com
- GitHub: @zameerkh
You can also open an issue on GitHub Issues if you encounter any problems or have feature requests.
Product | Versions 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Http.Polly (>= 8.0.8)
- Polly (>= 8.4.2)
- Polly.Extensions (>= 8.4.2)
- Refit (>= 7.2.1)
- Refit.HttpClientFactory (>= 7.2.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.