Dalisama.Extensions.Configuration 1.0.0

custom configuration provider for api

Install-Package Dalisama.Extensions.Configuration -Version 1.0.0
dotnet add package Dalisama.Extensions.Configuration --version 1.0.0
<PackageReference Include="Dalisama.Extensions.Configuration" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dalisama.Extensions.Configuration --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Dalisama.Extensions.Configuration, 1.0.0"
#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 Dalisama.Extensions.Configuration as a Cake Addin
#addin nuget:?package=Dalisama.Extensions.Configuration&version=1.0.0

// Install Dalisama.Extensions.Configuration as a Cake Tool
#tool nuget:?package=Dalisama.Extensions.Configuration&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Welcome to my humble readme

In this Repo, I implement a simple custom configuration provider that enable you to deliver and update your configuration via a Get api.
When you have a microservice architecture and you need to update and change configuration, it is a tedious work, you need to restart and update every microservices.
With tis NuGet, you only need to expose an APi that feed configuration to your service, and Voilà, you will be able to update your configuration in real time without the need of restarting anything.
This NuGet will support the use of Consul and any API that provide you config via Get, it supports all type of authentication, because simply you will need to provide your httpClient, so +1 for flexibility.

Lead by Example

Let me walk you through the first example:

Let’s first download this repository locally and you will need the dotnetcore runtime duh!
After that in the solution directory run this cmd:

dotnet build 

now you need to open two command line in the following
directory:

  1. \Dalisama.Extensions.Configuration.Api: this application will be providing the configuration via an api
  2. \Dalisama.Extensions.Configuration.Consumer: this application will be getting his configuration from the previous application

So you need to run this command in both command-lines:

dotnet run 

from you brower, try to access this link: https://localhost:5005/confconsumer and you will get this json:

[
  {
    "element1": 360,
    "element2": 454,
    "element3": 545,
    "element4": 1606
  },
  {
    "element1": 232,
    "element2": 1202,
    "element3": 757,
    "element4": 755
  }
]

You will notice if you refresh the link in the browser, the first element in this array will be changed but the last one will be the same, because actually it's the same object but when it's resolved with IOptionsSnapshot, the framework will fetch the last value, so here you get the flexibility of knowing the first value or the updated one.

[HttpGet]
public List<ClassOption> Get([FromServices] IOptionsSnapshot<ClassOption> option1, [FromServices] IOptions<ClassOption> option2)
{
    return new List<ClassOption> { option1.Value, option2.Value};
}

To get this result all you have to do is adding the nuget: Dalisama.Extensions.Configuration and updating your startup.cs:


   public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            Configuration = (IConfiguration)new ConfigurationBuilder().AddApiConfiguration(options =>
           {
           options.Url = "https://localhost:5001/ConfigurationProvider";
           options.ReloadOnChange = true;

               options.HttpClientFactory = () =>
               {
                   var handler = new HttpClientHandler();
                   handler.ServerCertificateCustomValidationCallback = (request, cert, chain, errors) =>
                    {
                        return errors == SslPolicyErrors.None;
                    };
                   return new HttpClient(handler);
               };
               options.ConfigKeyFormatter = (key, value) => key;
               options.ConfigValueFormatter = (key, value) => value;

           }).Build();
        }

and here in the ConfigureServices:


         public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.Configure<ClassOption>(Configuration.GetSection("Section1"));

        }

and Voilà!!!

Welcome to my humble readme

In this Repo, I implement a simple custom configuration provider that enable you to deliver and update your configuration via a Get api.
When you have a microservice architecture and you need to update and change configuration, it is a tedious work, you need to restart and update every microservices.
With tis NuGet, you only need to expose an APi that feed configuration to your service, and Voilà, you will be able to update your configuration in real time without the need of restarting anything.
This NuGet will support the use of Consul and any API that provide you config via Get, it supports all type of authentication, because simply you will need to provide your httpClient, so +1 for flexibility.

Lead by Example

Let me walk you through the first example:

Let’s first download this repository locally and you will need the dotnetcore runtime duh!
After that in the solution directory run this cmd:

dotnet build 

now you need to open two command line in the following
directory:

  1. \Dalisama.Extensions.Configuration.Api: this application will be providing the configuration via an api
  2. \Dalisama.Extensions.Configuration.Consumer: this application will be getting his configuration from the previous application

So you need to run this command in both command-lines:

dotnet run 

from you brower, try to access this link: https://localhost:5005/confconsumer and you will get this json:

[
  {
    "element1": 360,
    "element2": 454,
    "element3": 545,
    "element4": 1606
  },
  {
    "element1": 232,
    "element2": 1202,
    "element3": 757,
    "element4": 755
  }
]

You will notice if you refresh the link in the browser, the first element in this array will be changed but the last one will be the same, because actually it's the same object but when it's resolved with IOptionsSnapshot, the framework will fetch the last value, so here you get the flexibility of knowing the first value or the updated one.

[HttpGet]
public List<ClassOption> Get([FromServices] IOptionsSnapshot<ClassOption> option1, [FromServices] IOptions<ClassOption> option2)
{
    return new List<ClassOption> { option1.Value, option2.Value};
}

To get this result all you have to do is adding the nuget: Dalisama.Extensions.Configuration and updating your startup.cs:


   public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            Configuration = (IConfiguration)new ConfigurationBuilder().AddApiConfiguration(options =>
           {
           options.Url = "https://localhost:5001/ConfigurationProvider";
           options.ReloadOnChange = true;

               options.HttpClientFactory = () =>
               {
                   var handler = new HttpClientHandler();
                   handler.ServerCertificateCustomValidationCallback = (request, cert, chain, errors) =>
                    {
                        return errors == SslPolicyErrors.None;
                    };
                   return new HttpClient(handler);
               };
               options.ConfigKeyFormatter = (key, value) => key;
               options.ConfigValueFormatter = (key, value) => value;

           }).Build();
        }

and here in the ConfigureServices:


         public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.Configure<ClassOption>(Configuration.GetSection("Section1"));

        }

and Voilà!!!

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.0 158 8/30/2020
0.0.2-alpha-08142020 127 8/14/2020
0.0.1-alpha-08142020 134 8/14/2020