Just a couple of simple, extension methods I wrote to make app startup a little more concise. They are not included in ConfigurationExtensions by default, so they may have future use (or maybe there is a library I am missing that does this already).
The following methods are useful if you want the startup of your app to stop if it is not properly configured. They will throw an exception if they don’t find the corresponding config key.
If you have a value or object under a single config key, you can use GetRequiredValue
. This will make sure the section exists and then get the corresponding value. If the value does not exist, a ConfigurationNotSetException
will be thrown.
/// <summary>
/// Requires that the key and value be set when attempting to get the value, otherwise an exception of type
/// <see cref="ConfigurationNotSetException"/> will be thrown
/// </summary>
/// <param name="configuration">The current <see cref="IConfiguration"/></param>
/// <param name="key">The key of the value</param>
/// <typeparam name="T">The type of the value</typeparam>
/// <returns>The value</returns>
/// <exception cref="ConfigurationNotSetException">Thrown if the key or value is not set</exception>
public static T GetRequiredValue<T>(this IConfiguration configuration, string key)
{
var section = configuration.GetSection(key);
if (!section.Exists())
{
throw new ConfigurationNotSetException($"ConfigurationSection does not exist for key {key}");
}
var value = section.Get<T>();
if (value == null || (value is string s && string.IsNullOrWhiteSpace(s)))
{
throw new ConfigurationNotSetException($"Configuration value not set for key {key}");
}
return value;
}
If you have a connection string that you want to make sure is set, you can use GetRequiredConnectionString
. This will get the connection string from the ConnectionStrings
section of your appsettings
or will throw an exception if it is not set.
/// <summary>
/// Requires that the key and connection string be set, otherwise an exception of type
/// <see cref="ConfigurationNotSetException"/> will be thrown
/// </summary>
/// <param name="configuration">The current <see cref="IConfiguration"/></param>
/// <param name="key">The key for the "ConnectionStrings" section</param>
/// <returns>Connection string</returns>
/// <exception cref="ConfigurationNotSetException">Thrown if the key or connection string is not set</exception>
public static string GetRequiredConnectionString(this IConfiguration configuration, string key)
{
var section = configuration.GetSection("ConnectionStrings");
if (!section.Exists())
{
throw new ConfigurationNotSetException("ConnectionStrings section does not exist");
}
var value = configuration.GetConnectionString(key);
if (string.IsNullOrWhiteSpace(value))
{
throw new ConfigurationNotSetException($"ConnectionString not set for key {key}");
}
return value;
}
The exception class:
/// <summary>
/// Thrown when <see cref="ConfigurationExtensions.GetRequiredValue{T}"/> cannot find the specified key or value for
/// that key
/// </summary>
public sealed class ConfigurationNotSetException : Exception
{
public ConfigurationNotSetException(string? message) : base(message)
{
}
}