JPolicy 1.0.1
dotnet add package JPolicy --version 1.0.1
NuGet\Install-Package JPolicy -Version 1.0.1
<PackageReference Include="JPolicy" Version="1.0.1" />
paket add JPolicy --version 1.0.1
#r "nuget: JPolicy, 1.0.1"
// Install JPolicy as a Cake Addin #addin nuget:?package=JPolicy&version=1.0.1 // Install JPolicy as a Cake Tool #tool nuget:?package=JPolicy&version=1.0.1
JPolicy
JPolicy is a configurable error exception handler. It also includes definition of policies; just like to retry method execution and http request when failed, default error message display during the exception, and method to execute to get the verbose exception to display or for logging purposes
Requirements
- .NET Core 3.1
- Visual Studio or VS Code
How to use:
- Go to Tools and select Manage Nuget Packages and Search for JPolicy library
Install-Package JPolicy -Version 1.0.1
Exception Retry Policy:
using System;
using System.Collections.Generic;
namespace JPolicy.Test
{
class Program
{
static void Main(string[] args)
{
var p = new Program();
p.JWithMultiplePolicies();
p.JDefaultPolicy();
}
private void JWithMultiplePolicies()
{
int retryExecute = 2;
string defaultErrorMsg = "Opps! An error occured";
bool throwException = false;
JPolicy.ExceptionRetryPolicy(retryExecute, defaultErrorMsg, test => DisplayException(test), throwException)
.Execute(context => throw new Exception("I'm Inevitable"),
new Dictionary<string, object>() { { "InErrorMessage", $"name of the method/function {nameof(Program)}" } });
}
public void JDefaultPolicy()
{
JPolicy.ExceptionRetryPolicy().Execute(context => throw new Exception("I'm Inevitable"),
new Dictionary<string, object>() { { "InErrorMessage", $"name of the method/function {nameof(Program)}" } });
}
private static void DisplayException(object message)
{
Console.WriteLine(message);
}
}
}
Http Retry Policy Register service in Startup.cs and add the JPolicy.HttpRetryPolicy method in Add Policy Handler.
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient<TestService>(x =>
{
x.BaseAddress = new Uri(Configuration["TEST_BASE_URL"]);
}).AddPolicyHandler(JPolicy.HttpRetryPolicy());
}
TestService.cs
/// <summary>
/// This service will test the http retry policy.
/// </summary>
public class TestService
{
private readonly HttpClient _httpClient;
public TestService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<HttpResponseMessage> PostMessage(string postData)
{
StringContent content = new StringContent(postData, Encoding.UTF8, "application/json");
return await _httpClient.PostAsync("/test/api", content);
}
}
License
Copyright (c) 2021 Joever Monceda
Linkedin: Joever Monceda
Medium: Joever Monceda
Twitter @_EthanHunt07
Facebook: Ethan Hunt
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Polly (>= 7.2.1)
- Polly.Extensions.Http (>= 3.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.