Factory extensions to IConfiguration to enable configuration to use Kubernetes secrets files.
You can explicitly add the Kubernetes secret configs as follows:
builder
.AddJsonFile("appSettings.json", true)
.AddKubernetesSecrets()
.Build();
Or you can just use the extension we've build, UseDefaultConfigs
, as follows:
builder.UseDefaultConfigs().Build();
Using this second method will add all configuration in the following order:
- Kubernetes Secrets
- Environment Variables
- Json File (appsettings.json) [ignores if not found]
- Command Line Arguments
NOTE: When using the DefaultConfigs method, it can optionally add json files (automatically) for different environments using environment variables. The variable required
should be ENVIRONMENT
, and it would typically be set to dev in development scenarios but can equally be set to staging for staging specific config. Any config loaded
in this way will overwrite json file configs.
Extensions have been created to allow values to be added to the builder collection before being built. Individually, as follows:
builder.AddValue("MyKey", "MyValue");
Or as a collection of KeyValuePair's (IEnumerable<KeyValuePair<string, string>>
), as follows:
builder.AddValues(new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("testKey", "testVal")
});
Extensions have been added to allow values to be read from the builder before it's been built. Individually, as follows:
var configVal = builder.GetValue<int>("MyIntExample");
Or gather all configured settings before being built:
var allSettings = builder.GetAllSettingsAsString();
The equilivent for a build Configuration Builder (IConfiguration) would then be:
var config = builder.Build();
var allSettings = config.GetAllSettings(); // as collection of values
allSettingsString = config.GetAllSettingsAsString(); // as outputable string
An extension has been added to IConfiguration for attempting to get values:
var config = builder.Build();
var success = config.TryGetValue<int>("MyIntKey", out var myInt);
IConfiguration can only be bound to a type class when using the GetSection call. Meaning it has to be bound to a sub section of the config file, rather than the base section. It's sometimes useful to bind the base section values to a typed class. It can therefore be done as follows:
var config = builder.Build();
var myTypedInstance = config.BindBaseSection<AppSettings>();
An extension method that allows a memory cache item to be gathered from in memory cache or, if it does not already exist, to build and store the item.
IMemoryCache cache = new MemoryCache(new MemoryCacheOptions());
var cacheMe = "my cached piece of text";
// Will not exist already, therefore will build it.
var cachedItem = cache.GetOrBuild("key", () => cacheMe, TimeSpan.FromMinutes(30));
Will just get the previously cached item.
= cache.Get("key");
An additional Contains
method has also been added for convenience when checking if an item already exists in cache.
A threshold will be added to this package to ensure the test coverage is above 80% for branches, functions and lines. If it's not above the required threshold (threshold that will be implemented on ALL of the core repositories to gurantee a satisfactory level of testing), then the build will fail.
This package has has been written in .net Standard and can be therefore be referenced from a .net Core or .net Framework application. The advantage of utilising from a .net Core application, is that it can be deployed and run on a number of host operating systems, such as Windows, Linux or OSX. Unlike referencing from the a .net Framework application, which can only run on Windows (or Linux using Mono).
This package is built using .net Standard 2.1 and requires the .net Core 3.1 SDK, it can be downloaded here: https://www.microsoft.com/net/download/dotnet-core/
IDE of Visual Studio or Visual Studio Code, can be downloaded here: https://visualstudio.microsoft.com/downloads/
All of the Cloud.Core.* packages are published to a public NuGet feed. To consume this on your local development machine, please add the following feed to your feed sources in Visual Studio: https://dev.azure.com/cloudcoreproject/CloudCore/_packaging?_a=feed&feed=Cloud.Core
For help setting up, follow this article: https://docs.microsoft.com/en-us/vsts/package/nuget/consume?view=vsts