public static class ConfigurationExtensions { public static TConfig MapConfigSection<TConfig>(this IServiceCollection services, IConfiguration config, string name) where TConfig: class { var settings = config.GetSection(name).Get<TConfig>(); services.AddSingleton(settings); return settings; } }
services.MapConfigSection<PowerBISettings>(config, "PowerBI");
public class MySampleClass { private PowerBISettings _powerBiSettings; public MySampleClass( [FromServices] PowerBISettings powerBiSettings ) { _powerBiSettings = powerBiSettings; } }