web-app-template/backend/app/app.Configuration/ConfigurationExtensions.cs

37 lines
1.2 KiB
C#
Raw Normal View History

2022-02-20 13:43:25 +01:00
using Microsoft.Extensions.Configuration;
using System;
namespace app.Configuration {
public static class ConfigurationExtensions {
public static TSetting GetSetting<TSetting>(this IConfiguration config, string sectionName)
where TSetting : new() {
var settings = new TSetting();
config.GetSection(sectionName).Bind(settings);
return settings;
}
public static string GetDbConnectionString(this IConfiguration config, string sectionName) {
var connectionString = config[$"{sectionName}:ConnectionString"];
if (string.IsNullOrEmpty(connectionString))
throw new Exception($"ConnectionString is not set! SectionName:{sectionName}");
var dbCredentials = config[$"{sectionName}:Credentials"];
if (!string.IsNullOrEmpty(dbCredentials))
connectionString += dbCredentials;
return connectionString;
}
public static string GetCustomer(this IConfiguration config) {
return config["CUSTOMER"];
}
public static string GetComputerName(this IConfiguration config) {
return config["COMPUTERNAME"];
}
}
}