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 key foo
WebApp
System.Configuration
app.config
file containing the appSettings
section. (App.config
is by default present in a .NET Core 2.0 web application project.)Foo
, calls its GetSomething
method by which the value bar
gets assigned to the variable something
.All other files and settings are the default ones. Here below are the ones mentioned above.
.NET 4.5.2
Foo.cs
using System;
using System.Configuration;
namespace PFX
{
public class Foo
{
public String GetSomething()
{
String r = ConfigurationManager.AppSettings["foo"];
return r;
}
}
}
.NET Core 2.0.1 referencing full .NET framework 4.6.1.
WebApp.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.4" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.3" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyLibrary\MyLibrary.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Configuration" />
</ItemGroup>
</Project>
App.config
<configuration>
<runtime>
<gcServer enabled="true"/>
</runtime>
<appSettings>
<add key="foo" value="bar"/>
</appSettings>
</configuration>
Program.cs
using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace WebApp
{
public class Program
{
public static void Main(string[] args)
{
PFX.Foo foo = new PFX.Foo();
String someting = foo.GetSomething(); // bar
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(String[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With