在依赖 ConfigurationManager 的 .net core 2 中引用 .net 框架程序集
Referencing .net framework assemblies in .net core 2 that rely on the ConfigurationManager
我们有一些遗留的 4.5.2 class 库共同使用 ConfigurationManager.AppSettings[key]
是否可以在 .net core 2 应用程序中引用这些,以便在后台正确修补配置?
IE。对 ConfigurationManager.AppSettings[key]
的旧调用从 json 或 xml 正确读取了配置,但在 .netcore2 应用程序中。
如果我将问题的 keys
移植到 appSettings.json,那么对 ConfigurationManager.AppSettings
的调用总是 returns 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 class 库 "MyLibrary" 和一个 .NET Core 2.0 Web 应用程序 "WebApp" 引用了完整的 .NET Framework 4.6.1。
我的图书馆
- 具有对
System.Configuration
的程序集引用
- 有一个
class Foo
可以读取带有键 foo
的应用程序设置
WebApp
- 有一个项目引用 MyLibrary
- 具有对
System.Configuration
的程序集引用
- 有一个包含
appSettings
部分的 app.config
文件。 (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;
}
}
}
WebApp 项目
.NET Core 2.0.1 引用完整的 .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();
}
}
我们有一些遗留的 4.5.2 class 库共同使用 ConfigurationManager.AppSettings[key]
是否可以在 .net core 2 应用程序中引用这些,以便在后台正确修补配置?
IE。对 ConfigurationManager.AppSettings[key]
的旧调用从 json 或 xml 正确读取了配置,但在 .netcore2 应用程序中。
如果我将问题的 keys
移植到 appSettings.json,那么对 ConfigurationManager.AppSettings
的调用总是 returns 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 class 库 "MyLibrary" 和一个 .NET Core 2.0 Web 应用程序 "WebApp" 引用了完整的 .NET Framework 4.6.1。
我的图书馆
- 具有对
System.Configuration
的程序集引用
- 有一个
class Foo
可以读取带有键foo
的应用程序设置
WebApp
- 有一个项目引用 MyLibrary
- 具有对
System.Configuration
的程序集引用
- 有一个包含
appSettings
部分的app.config
文件。 (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;
}
}
}
WebApp 项目
.NET Core 2.0.1 引用完整的 .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();
}
}