Reputation: 16698
It’s more of an architecture related question. I am working on to extend.net framework application to add .net core support as well. Now my application is using System.Configuration.ConfigurationManager
class to read all the configurations and that class is important as some configurations related to reading dynamics modules are used at the time of bootstrapping the DI. Now I see .net core does not rely on this ConfigurationManager
class and has its own appsettings configuration file in JSON format. This would require me to change the strategy to have a common logic supporting both framework and core.
I am seeking any advice on how the community is addressing this problem and what is the best way forward.
App.config
<appSettings>
<add key="EndPointAssembliesPath" value="<some directory path>" />
<add key="ModulesPath" value="<some directory path>" />
<add key="BusinessAssembliesScanningPattern" value="*.BusinessModule.*.dll" />
</appSettings>
<connectionStrings>
<add name="CompanyDatabase" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=DataSource;Integrated Security=SSPI;" />
</connectionStrings>
appsettings.json
"EndPointAssembliesPath": "<some path>",
"BusinessModulesPath": "<some directory path>",
"BusinessAssembliesScanningPattern": "*.BusinessModule.*.dll",
"ConnectionStrings": {
"CompanyDatabase": "Data Source=.;Initial Catalog=DataSource;Integrated Security=SSPI;"
}
Upvotes: 1
Views: 2524
Reputation: 5384
Please take a look at Configuration providers in .NET. All the samples are intended to be used with .NET Core, but most of the NuGet packages are compatible with .NET Standard 2.0. Therefore you should be able to consume them from any .NET Framework application being compatible to .NET Standard 2.0.
Your code could look a bit like this:
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddXmlFile("App.config", optional: true, reloadOnChange: true);
var config = configurationBuilder.Build();
From that on, you can work with the unified config
object.
Disclaimer: I didn't test it, but I'm quite confident 😄
Upvotes: 3
Reputation: 420
Check if the app is using a web.config file or a json file. An example for a connection string:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (ConfigurationManager.ConnectionStrings["DefaultConnection"] != null)
{
optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
}
else
{
var connString = AppConfigSettings.Current.AppConnection;
optionsBuilder.UseSqlServer(connString);
}
}
Upvotes: -1