JPolicy 1.0.1

Method and Http retry exception base on configurations.

Install-Package JPolicy -Version 1.0.1
dotnet add package JPolicy --version 1.0.1
<PackageReference Include="JPolicy" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JPolicy --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: JPolicy, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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

License: MIT
Copyright (c) 2021 Joever Monceda

Linkedin: Joever Monceda
Medium: Joever Monceda
Twitter @_EthanHunt07
Facebook: Ethan Hunt

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

License: MIT
Copyright (c) 2021 Joever Monceda

Linkedin: Joever Monceda
Medium: Joever Monceda
Twitter @_EthanHunt07
Facebook: Ethan Hunt

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.1 79 5/17/2021
1.0.0 110 11/25/2020