All Projects → wintoncode → Winton.extensions.configuration.consul

wintoncode / Winton.extensions.configuration.consul

Licence: other
Enables Consul to be used as a configuration source in dotnet core applications

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Winton.extensions.configuration.consul

Simple Settings
A simple way to manage your project settings.
Stars: ✭ 165 (-30.96%)
Mutual labels:  config, consul, configuration
Netcorebbs
ASP.NET Core Light forum NETCoreBBS
Stars: ✭ 483 (+102.09%)
Mutual labels:  asp-net-core, dotnet-core, netcore
Netcorecms
NetCoreCMS is a modular theme supported Content Management System developed using ASP.Net Core 2.0 MVC. Which is also usable as web application framework. This project is still under development. Please do not use before it's first release.
Stars: ✭ 165 (-30.96%)
Mutual labels:  asp-net-core, dotnet-core, netcore
Aspnetcore.services
Examples for ASP.NET Core webservices
Stars: ✭ 59 (-75.31%)
Mutual labels:  asp-net-core, consul, configuration
Simplcommerce
A simple, cross platform, modularized ecommerce system built on .NET Core
Stars: ✭ 3,474 (+1353.56%)
Mutual labels:  asp-net-core, dotnet-core, netcore
Raft.net
Implementation of RAFT distributed consensus algorithm among TCP Peers on .NET / .NETStandard / .NETCore / dotnet
Stars: ✭ 112 (-53.14%)
Mutual labels:  dotnet-core, netcore, net
Konf
A type-safe cascading configuration library for Kotlin/Java/Android, supporting most configuration formats
Stars: ✭ 225 (-5.86%)
Mutual labels:  config, configuration
Ketchup
ketchup (番茄酱) 是一个基于dotnet core的微服务框架。
Stars: ✭ 170 (-28.87%)
Mutual labels:  consul, dotnet-core
Angular 7 Project With Asp.net Core Apis
Angular 7 Project with ASP.NET CORE APIS | Angular Project
Stars: ✭ 174 (-27.2%)
Mutual labels:  asp-net-core, dotnet-core
Anclafs
ASP.NET Core Library and Framework Support
Stars: ✭ 192 (-19.67%)
Mutual labels:  asp-net-core, dotnet-core
Storedprocedureefcore
Entity Framework Core extension to execute stored procedures
Stars: ✭ 164 (-31.38%)
Mutual labels:  asp-net-core, dotnet-core
Hotchocolate
Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
Stars: ✭ 3,009 (+1159%)
Mutual labels:  asp-net-core, dotnet-core
Templates
.NET project templates with batteries included, providing the minimum amount of code required to get you going faster.
Stars: ✭ 2,864 (+1098.33%)
Mutual labels:  asp-net-core, dotnet-core
Mqttnet
MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
Stars: ✭ 2,486 (+940.17%)
Mutual labels:  netcore, net
Config
Various program configuration files and scripts
Stars: ✭ 173 (-27.62%)
Mutual labels:  config, configuration
Aconfig
Simple, useful and opinionated config loader.
Stars: ✭ 187 (-21.76%)
Mutual labels:  config, configuration
Remco
remco is a lightweight configuration management tool
Stars: ✭ 200 (-16.32%)
Mutual labels:  config, consul
Awesome Wpf
A collection of awesome WPF resources, libraries and UI controls.
Stars: ✭ 196 (-17.99%)
Mutual labels:  dotnet-core, net
Config
🛠 A configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP
Stars: ✭ 203 (-15.06%)
Mutual labels:  config, configuration
Environ Config
Python Application Configuration With Environment Variables
Stars: ✭ 210 (-12.13%)
Mutual labels:  config, configuration

Winton.Extensions.Configuration.Consul

Appveyor Travis CI NuGet version NuGet version

Adds support for configuring .NET Core applications using Consul. Works great with git2consul.

Installation

Add Winton.Extensions.Configuration.Consul to your project's dependencies, either via the NuGet package manager or as a PackageReference in the csproj file.

Usage

Minimal Setup

The library provides an extension method called AddConsul for IConfigurationBuilder in the same way that other configuration providers do. The IConfigurationBuilder is usually configured in either the Program or Startup class for an ASP.NET Core application. See Microsoft's documentation for more information about IConfigurationBuilder.

A minimal example is shown below:

builder
    .AddConsul($"{env.ApplicationName}/{env.EnvironmentName}");

Assuming the application is running in the 'Development' environment and the application name is 'Website', then this will load a JSON configuration object from the Website/Development key in Consul.

Options

AddConsul has an overload with an additional third parameter of type Action<IConsulConfigurationSource> which allows the options outlined below to be set.

  • ConsulConfigurationOptions

    An Action<ConsulClientConfiguration> that can be used to configure the underlying Consul client.

  • ConsulHttpClientHandlerOptions

    An Action<HttpClientHandler> that can be used to configure the underlying Consul client's HTTP handler options.

  • ConsulHttpClientOptions

    An Action<HttpClient> that can be used to configure the underlying Consul client's HTTP options.

  • KeyToRemove

    The portion of the Consul key to remove from the configuration keys. By default, when the configuration is parsed, the keys are created by removing the root key in Consul where the configuration is located. This defaults to Key.

  • OnLoadException

    An Action<ConsulLoadExceptionContext> that can be used to configure how exceptions thrown during the first load should be handled.

  • OnWatchException

    A Func<ConsulWatchExceptionContext, TimeSpan> that can be used to configure how exceptions thrown when watching for changes should be handled. The TimeSpan that is returned is used to set a delay before retrying. The ConsulWatchExceptionContext provides data that can be used to implement a back-off strategy or to cancel watching altogether.

  • Optional

    A bool that indicates whether the config is optional. If false then it will throw during the first load if the config is missing for the given key. Defaults to false.

  • Parser

    The parser to use, which should match the format of the configuration stored in Consul. Defaults to JsonConfigurationParser. Either use those under Winton.Extensions.Configuration.Consul.Parsers or create your own by implementing IConfigurationParser.

  • PollWaitTime

    The amount of time the client should wait before timing out when polling for changes. If this is set too low it can lead to excessive requests being issued to Consul. Note this setting does not affect how quickly updates propagate, because when a value changes the long polling query returns immediately. It is better to think of this as the frequency with which it issues calls in the long polling loop in the case where there is no change. Defaults to 5 minutes.

  • ReloadOnChange

    A bool indicating whether to reload the config when it changes in Consul. If true it will watch the configured key for changes. When a change occurs the config will be asynchronously reloaded and the IChangeToken will be triggered to signal that the config has been reloaded. Defaults to false.

  • ConvertConsulKVPairToConfig

    A Func<KVPair, IEnumerable<KeyValuePair<string, string>>> which gives you complete control over the parsing of fully qualified consul keys and raw consul values; the default implementation will:

    • Use the configured Parser to parse consul values
    • Remove the configured KeyToRemove prefix from consul keys

    When setting this member, however, you bypass the default key and value processing and Parser and KeyToRemove have no effect unless your ConvertConsulKVPairToConfig function uses them.

Configure Parsing Options

Consul values are JSON

By default this configuration provider will load all key-value pairs from Consul under the specified root key, but by default it assumes that the values of the leaf keys are encoded as JSON.

Take the following example of a particular instance of the Consul KV store:

- myApp/
    - auth/
        {
            "appId": "guid",
            "claims": [
                "email",
                "name"
            ]
        }
    - logging/
        {
            "level": "warn"
        }

In this instance we could add Consul as a configuration source like so:

var configuration = builder
    .AddConsul("myApp", cancellationToken)
    .Build();

The resultant configuration would contain sections for auth and logging. As a concrete example configuration.GetValue<string>("logging:level") would return "warn" and configuration.GetValue<string>("auth:claims:0") would return "email".

Consul values are scalars

Sometimes however, config in Consul is stored as a set of expanded keys. For instance, tools such as consul-cli load config in this format.

The config in this case can be thought of as a tree under a specific root key in Consul. For instance, continuing with the example above, the config would be stored as:

- myApp/
    - auth/
        - appId/
            "guid"
        - claims/
            0/
                "email"
            1/
                "name"
    - logging/
        - level/
            "warn"

As outlined above this configuration provider deals with recursive keys by default. The only difference here is that the values are no longer encoded as JSON. Therefore, in order to load this config the parser must be changed. This can be done like so when adding the configuration provider:

builder
    .AddConsul(
        "myApp",
        options =>
        {
            options.Parser = new SimpleConfigurationParser();
        });

The SimpleConfigurationParser expects to encounter a scalar value at each leaf key in the tree.

Consul values are a mix of JSON and scalars

If you need to support both expanded keys and JSON values then this can be achieved by putting them under different root keys and adding multiple configuration sources. For example:

builder
    .AddConsul(
        "myApp/expandedKeys",
        options =>
        {
            options.Parser = new SimpleConfigurationParser();
        })
    .AddConsul("myApp/jsonValues", cancellationToken);

Customizing the ConvertConsulKVPairToConfig strategy

Sometimes you may need more control over the conversion of raw consul KV pairs into IConfiguration data. In this case you can set a custom ConvertConsulKVPairToConfig function:

builder
    .AddConsul(
        "myApp",
        options =>
        {
            options.ConvertConsulKVPairToConfig = kvPair =>
            {
                var normalizedKey = kvPair.Key
                    .Replace("base/key", string.Empty)
                    .Replace("__", "/")
                    .Replace("/", ":")
                    .Trim('/');

                using Stream valueStream = new MemoryStream(kvPair.Value);
                using var streamReader = new StreamReader(valueStream);
                var parsedValue = streamReader.ReadToEnd();

                return new Dictionary<string, string>()
                {
                    { normalizedKey, parsedValue }
                };
            };
        });

⚠️ Caution: by customizing this ConvertConsulKVPairToConfig strategy you bypass any automatic invocation of the configured Parser and KeyToRemove so it becomes your responsibility to use them as needed by your scenario.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].