Adds support for configuring .Net Core application using Consul. It is expected that the configuration will be stored as a single object under a given key in Consul. Works great with git2consul.
Add Winton.Extensions.Configuration.Consul to the dependencies section of your project.json
Add the following to your StartUp class for the minimal setup:
var cancellationTokenSource = new cancellationTokenSource();
var builder = new ConfigurationBuilder()
.AddConsul(
$"{env.ApplicationName}.{env.EnvironmentName}",
cancellationTokenSource.Token);
Configuration = builder.Build();Assuming the application is running in the development environment and the application name is Website, this will load a json configuration object from the key Website/Development in Consul.
The CancellationToken is used to cancel any current requests or watches with Consul.
It is recommended that this is cancelled during application shut down to clean up resources. This can be done like so in the Configure method of your StartUp class by injecting the IApplicationLifetime:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
{
// Other app configuration
appLifetime.ApplicationStopping.Register(_cancellationTokenSource.Cancel);
}An options Action can be specified as a third argument to set the options outlined below.
-
ConsulConfigurationOptionsAn
Actionthat can be used to configure Consul options -
ConsulHttpClientOptionsAn
Actionthat can be used to configure Consul HTTP options -
ConsulHttpClientHandlerOptionsAn
Actionthat can be used to configure Consul HTTP handler options -
OnLoadExceptionAn
Actionthat can be used to configure how exceptions should be handled during load -
OnLoadExceptionAn
Actionthat can be used to configure how exceptions should be handled that are thrown when watching for changes -
OptionalA
boolthat indicates whether the config is optional. Iffalsethen will throw during load if the config is missing for the given key. -
ParserThe parser to use, should match the format of the configuration stored in Consul. Defaults to
JsonConfigurationParser. Either use those underWinton.Extensions.Configuration.Consul.Parsersor create your own by implementingIConfigurationParser. -
ReloadOnChangeA
boolindicating whether to reload the config when it changes in Consul. Iftrueit will watch the configured key for changes and then reload the config asynchronously and trigger theIChangeTokento raise the event that the config has been reloaded.
- Add more parsers for different file formats
- Add support for expanded configuration where the configuration is a tree of KV pairs under the root key