8

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]
boro2g
  • 524
  • 4
  • 16
  • I accept this isn't the way config _should_ be handled in .netcore2 however was trying to gauge how much of the legacy code base would work without alteration. – boro2g Sep 01 '17 at 15:51
  • Did you solve this? – Olaj May 31 '18 at 08:35
  • Nope, we have yet to make the move to .net core for these projects. – boro2g Jun 01 '18 at 08:55
  • This may help https://stackoverflow.com/questions/50258251/net-core-2-0-appsettings-with-net-full-framework-using-configurationmanager – Alex Terry Jun 01 '18 at 13:46
  • @ATerry Wouldn't that require the .NET 4.5.2 project to be upgraded to .NET 4.6.1 as 4.5.x is not compatible with .NET Core/Standard 2? – pfx Jun 01 '18 at 19:50

1 Answers1

6

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

  • has an assembly reference to System.Configuration
  • has a class Foo which reads an appsetting with key foo

WebApp

  • has a project reference to MyLibrary
  • has an assembly reference to System.Configuration
  • has an app.config file containing the appSettings section. (App.config is by default present in a .NET Core 2.0 web application project.)
  • uses in instance of 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.


MyLibrary project

.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;
        }
    }
}

WebApp project

.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();
    }
}
pfx
  • 20,323
  • 43
  • 37
  • 57