【问题标题】:Referencing .net framework assemblies in .net core 2 that rely on the ConfigurationManager引用 .net core 2 中依赖于 ConfigurationManager 的 .net 框架程序集
【发布时间】:2018-02-10 17:38:23
【问题描述】:

我们有一些旧的 4.5.2 类库,它们共同使用 ConfigurationManager.AppSettings[key]

是否可以在 .net core 2 应用程序中引用这些,以便在后台正确修补配置? IE。对 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"
}

然后:

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

将显示:

["value1","value2",null,null]

【问题讨论】:

  • 我接受这不是在 .netcore2 中处理配置应该的方式,但它试图衡量有多少遗留代码库可以在不改变的情况下工作。
  • 你解决了吗?
  • 不,我们还没有为这些项目迁移到 .net 核心。
  • @ATerry 是否需要将 .NET 4.5.2 项目升级到 .NET 4.6.1,因为 4.5.x 与 .NET Core/Standard 2 不兼容?

标签: asp.net configuration .net-core-2.0


【解决方案1】:

您正在寻找的设置是可能的,并且设置可以在 app.config 中保持原样。

我有一个 .NET 4.5.2 类库“MyLibrary”和一个引用完整 .NET 框架 4.6.1 的 .NET Core 2.0 Web 应用程序“WebApp”。

我的图书馆

  • 具有对System.Configuration 的程序集引用
  • 有一个class Foo,它读取一个带有键foo的应用程序设置

网络应用

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

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


我的图书馆项目

.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 项目

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

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();
    }
}

【讨论】:

    猜你喜欢
    • 2018-07-16
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多