Additional configuration providers to use with ASP.NET Core Microsoft.Extensions.Configuration
.
A YAML configuration provider that uses YamlDotNet to load and parse your YAML files.
Install using the NetEscapades.Configuration.Yaml NuGet package:
PM> Install-Package NetEscapades.Configuration.Yaml
When you install the package, it should be added to your package.json
. Alternatively, you can add it directly by adding:
{
"dependencies" : {
"NetEscapades.Configuration.Yaml": "1.1.0"
}
}
To load a YAML file as part of your config, just load it as part of your normal ConfigurationBuilder
setup in the Startup
class of your ASP.NET Core app.
The simplest possible usage that loads a single YAML file called appsettings.yml
would be:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddYamlFile("appsettings.yml", optional: false);
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
}
A more complete Startup
class that loads multiple files (overwriting config values) might look more like the following:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddYamlFile("appsettings.yml", optional: false)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}
There is a demo Web API project in the test folder of the GitHub project at https://github.com/andrewlock/NetEscapades.Configuration/tree/master/test/WebDemoProject
One thing to be aware of is that the YAML specification is case sensitive, so the following file is valid and has 3 distinct keys:
test: Value1
Test: Value2
TEST: Value3
However, the Microsoft.Extensions.Configuration
library is case insensitive. Attempting to load the provided file would throw an exception on attempting to load, compaining of a duplicate key.
A Remote configuration provider that loads configuration from a remote endpoint.
Install using the NetEscapades.Configuration.Remote NuGet package:
PM> Install-Package NetEscapades.Configuration.Remote
###Usage
When you install the package, it should be added to your package.json
. Alternatively, you can add it directly by adding:
{
"dependencies" : {
"NetEscapades.Configuration.Remote": "0.1.0"
}
}
To load a file from a remote configuration source as part of your config, just load it as part of your normal ConfigurationBuilder
setup in the Startup
class of your ASP.NET Core app.
The simplest possible usage that loads a single json file from a remote source http://localhost:5000
would be:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.AddRemoteSource(new Uri("http://localhost"), optional: false);
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
}
There are a number of properties available for configuration on the RemoteConfigurationSource
which allow customising the call to the remote endpoint:
ConfigurationKeyPrefix
- All Keys loaded from the source will be prefixed with this key"prefix"
and"prefix:123"
are valid prefixes, so a key loaded as<"key", "value">
will be added to the configuration as<"prefix:123:key", <value>"
.MediaType
- the media type that the remote source will send, by default"application/json"
Parser
- anIConfigurationParser
that will be used to parse the response. AJsonConfigurationParser
is included which is taken from the Microsoft.Extensions.Configuration.Json pacakge source code.
The Events
object provides hooks before sending the request using OnSendingRequest
and after the data has been processed using OnDataParsed
.
If the remote source does not return a success response, it will throw an exception, unless you set the Optional
flag to true.