Last active
March 6, 2024 07:16
-
-
Save unseensenpai/a16e7f076dec0e91459e8b3e67a5fc8b to your computer and use it in GitHub Desktop.
How To Register App.Config and appsettings.json -> Config Manager for .Net 6+ and ASP.NET Core 6+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<configuration> | |
<appSettings> | |
<add key="Bla:Foo:Zoo" value="SampleValue" /> | |
</appSettings> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"ConnectionStrings": { | |
"Default": "123456" | |
}, | |
"SampleInt": 1234567, | |
"SampleStringInt": "1234567" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ApiController] | |
[Route("[Controller]/[Action]")] | |
public class ConfigController : Controller | |
{ | |
[HttpGet] | |
public IList<ConfigKeyValue> GetConfigs() | |
{ | |
// HOW TO READ CONFIGS ON ASP.NET CORE WEB API (IT HAS OWN DI BUT SOME REASON ITS USABLE FOR IT THO) | |
return ConfigManager.ConfigurationKeyValues.ToList(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ConfigManager | |
{ | |
public static IConfiguration Configuration { get; private set; } | |
public static IEnumerable<ConfigKeyValue> ConfigurationKeyValues { get => Configuration.AsEnumerable().Select(cfg => new ConfigKeyValue(cfg.Key, cfg.Value)); } | |
/// <summary> | |
/// Create configuration | |
/// </summary> | |
/// <param name="configFilePaths">Json configs (appsettings.json)</param> | |
/// <param name="nameValueCollections">Xml configs (App.config)</param> | |
/// <returns></returns> | |
public static IConfiguration CreateConfiguration(List<ConfigFilePaths> configFilePaths, List<NameValueCollection> nameValueCollections = default) | |
{ | |
ConfigurationBuilder configBuilder = new(); | |
// APPSETTINGS.JSON | |
if (configFilePaths is not null && configFilePaths.Count > 0) | |
{ | |
foreach (var configFilePath in configFilePaths) | |
{ | |
configBuilder.AddJsonFile( | |
path: configFilePath.JsonPath, | |
optional: configFilePath.Optional, | |
reloadOnChange: configFilePath.ReloadOnChange); | |
} | |
} | |
// APP.CONFIG | |
if (nameValueCollections is not null && nameValueCollections.Count > 0) | |
{ | |
foreach (var nameValueCollection in nameValueCollections) | |
{ | |
var keyValues = nameValueCollection.AllKeys.ToDictionary(key => key, key => nameValueCollection[key]); | |
configBuilder.AddInMemoryCollection(keyValues); | |
} | |
} | |
return configBuilder.Build(); | |
} | |
/// <summary> | |
/// Create configuration | |
/// </summary> | |
/// <param name="configFilePaths">Json configs (appsettings.json)</param> | |
/// <param name="nameValueCollections">Xml configs (App.config)</param> | |
/// <returns></returns> | |
public static void SetConfiguration(List<ConfigFilePaths> configFilePaths, List<NameValueCollection> nameValueCollections = default) | |
{ | |
ConfigurationBuilder configBuilder = new(); | |
// APPSETTINGS.JSON | |
if (configFilePaths is not null && configFilePaths.Count > 0) | |
{ | |
foreach (var configFilePath in configFilePaths) | |
{ | |
configBuilder.AddJsonFile( | |
path: configFilePath.JsonPath, | |
optional: configFilePath.Optional, | |
reloadOnChange: configFilePath.ReloadOnChange); | |
} | |
} | |
// APP.CONFIG | |
if (nameValueCollections is not null && nameValueCollections.Count > 0) | |
{ | |
foreach (var nameValueCollection in nameValueCollections) | |
{ | |
var keyValues = nameValueCollection.AllKeys.ToDictionary(key => key, key => nameValueCollection[key]); | |
configBuilder.AddInMemoryCollection(keyValues); | |
} | |
} | |
Configuration = configBuilder.Build(); | |
} | |
} | |
public record ConfigFilePaths(string JsonPath, bool Optional = false, bool ReloadOnChange = true); | |
public record ConfigKeyValue(string Key, string Value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = WebApplication.CreateBuilder(args); | |
// HOW TO REGISTER ALL CONFIGS TO ASP.NET CORE DI (EITHER APP.CONFIG or APPSETTINGS.JSON) | |
ConfigManager.SetConfiguration([new("appsettings.json", true, true), new("appsettings.development.json", true, true)]) | |
builder.Configuration.AddInMemoryCollection(ConfigManager.ConfigurationKeyValues.Select(cm => new KeyValuePair<string, string>(cm.Key, cm.Value))); | |
var app = builder.Build(); | |
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// The main entry point for the application. | |
/// </summary> | |
[STAThread] | |
static void Main() | |
{ | |
List<ConfigFilePaths> configFilePaths = [ | |
new ConfigFilePaths("appsettings.json", true), | |
new ConfigFilePaths("Config/appsettings.json"), | |
new ConfigFilePaths($"appsettings.development.json", true) | |
]; | |
List<NameValueCollection> nameValueCollections = [System.Configuration.ConfigurationManager.AppSettings]; | |
ConfigManager.SetConfiguration(configFilePaths, nameValueCollections); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public WinFormUsage() | |
{ | |
InitializeComponent(); | |
// SOME USE CASES ON WINFORM FORMS | |
gridControl1.DataSource = ConfigManager.ConfigurationKeyValues; | |
string connString = ConfigManager.Configuration.GetConnectionString("Default"); | |
int sampleInt = Convert.ToInt32(ConfigManager.Configuration["SampleInt"]); | |
int sampleIntWithGetValue = ConfigManager.Configuration.GetValue<int>("SampleInt"); | |
int sampleInt2 = ConfigManager.Configuration.GetValue<int>("SampleStringInt"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment