85b362e8cd
- add the standalone HrynCo.Common solution and projects - include the shared common library source and tests - add package metadata and a repo gitignore
37 lines
1.2 KiB
C#
37 lines
1.2 KiB
C#
namespace HrynCo.Common;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
public static class ConfigurationFactory
|
|
{
|
|
/// <summary>
|
|
/// Use for .NET Core Console applications.
|
|
/// </summary>
|
|
public static IConfigurationRoot CreateConfiguration()
|
|
{
|
|
return CreateConfiguration(AppContext.BaseDirectory);
|
|
}
|
|
|
|
public static IConfigurationRoot CreateConfiguration(string basePath)
|
|
{
|
|
string environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
|
|
return CreateConfiguration(basePath, environmentName);
|
|
}
|
|
|
|
public static IConfigurationRoot CreateConfiguration(string basePath, string environmentName)
|
|
{
|
|
if (!Directory.Exists(basePath))
|
|
{
|
|
throw new DirectoryNotFoundException(
|
|
$"Directory {basePath} does not exist. Wrong value of the {nameof(basePath)} parameter.");
|
|
}
|
|
|
|
return new ConfigurationBuilder()
|
|
.SetBasePath(basePath)
|
|
.AddJsonFile("appsettings.json", false, true)
|
|
.AddJsonFile($"appsettings.{environmentName}.json", true, true)
|
|
.AddJsonFile("local.settings.json", true)
|
|
.AddEnvironmentVariables()
|
|
.Build();
|
|
}
|
|
} |