big changes
This commit is contained in:
@@ -7,28 +7,102 @@ public static class AppSettingsLoader
|
||||
{
|
||||
public static AppSettings Load()
|
||||
{
|
||||
// Zuerst Output-Verzeichnis, dann aktuelles Arbeitsverzeichnis (z.B. VS Debug).
|
||||
string basePath = Directory.Exists(AppContext.BaseDirectory)
|
||||
? AppContext.BaseDirectory
|
||||
: Directory.GetCurrentDirectory();
|
||||
|
||||
string settingsPath = Path.Combine(basePath, "appsettings.json");
|
||||
|
||||
if (!File.Exists(settingsPath))
|
||||
{
|
||||
string cwdPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
|
||||
string cwdPath = Path.Combine(
|
||||
Directory.GetCurrentDirectory(),
|
||||
"appsettings.json");
|
||||
|
||||
if (File.Exists(cwdPath))
|
||||
{
|
||||
basePath = Directory.GetCurrentDirectory();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new FileNotFoundException(
|
||||
"Die Datei appsettings.json wurde nicht gefunden.",
|
||||
settingsPath);
|
||||
}
|
||||
}
|
||||
|
||||
IConfigurationRoot configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(basePath)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
|
||||
.AddJsonFile(
|
||||
"appsettings.json",
|
||||
optional: false,
|
||||
reloadOnChange: false)
|
||||
.Build();
|
||||
|
||||
AppSettings settings = new();
|
||||
configuration.Bind(settings);
|
||||
|
||||
Validate(settings);
|
||||
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
|
||||
private static void Validate(
|
||||
AppSettings settings)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(settings.EnvironmentCode))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"EnvironmentCode fehlt in appsettings.json.");
|
||||
}
|
||||
|
||||
settings.EnvironmentCode =
|
||||
settings.EnvironmentCode
|
||||
.Trim()
|
||||
.ToUpperInvariant();
|
||||
|
||||
string[] allowedEnvironmentCodes =
|
||||
[
|
||||
"DEV",
|
||||
"TEST",
|
||||
"PROD"
|
||||
];
|
||||
|
||||
if (!allowedEnvironmentCodes.Contains(
|
||||
settings.EnvironmentCode,
|
||||
StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"EnvironmentCode '{settings.EnvironmentCode}' ist ungültig. " +
|
||||
"Erlaubt sind DEV, TEST und PROD.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(
|
||||
settings.Database.ConnectionString))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Database:ConnectionString fehlt in appsettings.json.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(
|
||||
settings.Runtime.JavaExecutablePath))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Runtime:JavaExecutablePath fehlt in appsettings.json.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(
|
||||
settings.Runtime.SonicClientLibraryPath))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Runtime:SonicClientLibraryPath fehlt in appsettings.json.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(
|
||||
settings.AlwaysEncrypted.CertificateThumbprint))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"AlwaysEncrypted:CertificateThumbprint fehlt.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user