引用依赖于ConfigurationManager的.net core 2中的.net框架程序集

bor*_*o2g 8 asp.net configuration .net-core-2.0

我们有一些常用的4.5.2类库 ConfigurationManager.AppSettings[key]

是否可以在.net core 2应用程序中引用这些内容,以便在引擎盖下正确修补配置?即旧的调用ConfigurationManager.AppSettings[key]正确读取配置,从json或xml,但在.netcore2应用程序内.

如果我将keys问题移植到appSettings.json,则调用ConfigurationManager.AppSettings始终返回null.

一个例子是:

{
"Logging": {
    "IncludeScopes": false,
    "Debug": {
        "LogLevel": {
            "Default": "Warning"
        }
    },
    "Console": {
        "LogLevel": {
            "Default": "Warning"
        }
    }
},
"appSettings": {"test": "bo"},
"test": "hi"
}
Run Code Online (Sandbox Code Playgroud)

然后:

[HttpGet]
public IEnumerable<string> Get()
{
    return new string[] { "value1", "value2", ConfigurationManager.AppSettings["test"] , ConfigurationManager.AppSettings["appSettings:test"] };
}
Run Code Online (Sandbox Code Playgroud)

将显示:

["value1","value2",null,null]
Run Code Online (Sandbox Code Playgroud)

pfx*_*pfx 6

您正在寻找的设置是可能的,设置可以保存在app.config中.

我有一个.NET 4.5.2类库"MyLibrary"和一个.NET Core 2.0 Web应用程序"WebApp",它引用了完整的.NET框架4.6.1.

我的图书馆

  • 有一个汇编引用 System.Configuration
  • 有一个class Foo用键读取appsettingfoo

Web应用程序

  • 有一个项目参考MyLibrary
  • 有一个汇编引用 System.Configuration
  • 有一个app.config包含该appSettings部分的文件.(App.config默认情况下存在于.NET Core 2.0 Web应用程序项目中.)
  • 在实例中使用Foo,调用其GetSomething方法,通过该方法将值bar赋给变量something.

所有其他文件和设置都是默认值.以下是上面提到的那些.


MyLibrary项目

.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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

WebApp项目

.NET Core 2.0.1引用完整的.NET框架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>
Run Code Online (Sandbox Code Playgroud)

App.config中

<configuration>
    <runtime>
       <gcServer enabled="true"/>
    </runtime>

    <appSettings>
        <add key="foo" value="bar"/>
    </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)