using Microsoft.Extensions.Configuration; using System; namespace gswi.Configuration { public static class ConfigurationExtensions { public static TSetting GetSetting(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"]; } } }