Asp.net

引用 .net core 2 中依賴於 ConfigurationManager 的 .net 框架程序集

  • June 1, 2018

我們有一些遺留的 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"
}

接著:

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

將顯示:

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

您正在尋找的設置是可能的,並且設置可以在 app.config 中保持原樣。

我有一個 .NET 4.5.2 類庫“MyLibrary”和一個引用完整 .NET 框架 4.6.1 的 .NET Core 2.0 Web 應用程序“WebApp”。

我的圖書館

  • 有一個程序集引用System.Configuration
  • 有一個class Foo用 key 讀取 appsetting 的foo

網路應用

  • 有一個對 MyLibrary 的項目引用
  • 有一個程序集引用System.Configuration
  • 有一個app.config包含該appSettings部分的文件。(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;
       }
   }
}

網路應用項目

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

應用程序配置

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

   <appSettings>
       <add key="foo" value="bar"/>
   </appSettings>
</configuration>

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

引用自:https://stackoverflow.com/questions/46003901