We have some legacy 4.5.2 class libraries that make common use of ConfigurationManager.AppSettings[key]
Is it possible to reference these within a .net core 2 application so that the config is correctly patched up under the hood?
I.e. the old calls into ConfigurationManager.AppSettings[key]
correctly read the config, either from json or xml, but within the .netcore2 app.
If I port the keys
of question to appSettings.json then the call into ConfigurationManager.AppSettings
always returns null.
An example would be:
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"appSettings": {"test": "bo"},
"test": "hi"
}
And then:
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2", ConfigurationManager.AppSettings["test"] , ConfigurationManager.AppSettings["appSettings:test"] };
}
Will display:
["value1","value2",null,null]
The setup you are looking for is possible and the settings can be kept as-is in app.config.
I have a .NET 4.5.2 class library "MyLibrary" and a .NET Core 2.0 web application "WebApp" referencing the full .NET framework 4.6.1.
MyLibrary
System.Configuration
class Foo
which reads an appsetting with keyfoo
WebApp
System.Configuration
app.config
file containing theappSettings
section. (App.config
is by default present in a .NET Core 2.0 web application project.)Foo
, calls itsGetSomething
method by which the valuebar
gets assigned to the variablesomething
.All other files and settings are the default ones. Here below are the ones mentioned above.
MyLibrary project
.NET 4.5.2
Foo.cs
WebApp project
.NET Core 2.0.1 referencing full .NET framework 4.6.1.
WebApp.csproj
App.config
Program.cs