[英]Referencing .net framework assemblies in .net core 2 that rely on the ConfigurationManager
We have some legacy 4.5.2 class libraries that make common use of ConfigurationManager.AppSettings[key]
我们有一些遗留的4.5.2类库,它们共同使用
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? 是否可以在.net core 2应用程序中引用这些内容,以便在引擎盖下正确修补配置? Ie the old calls into
ConfigurationManager.AppSettings[key]
correctly read the config, either from json or xml, but within the .netcore2 app. 即旧的调用
ConfigurationManager.AppSettings[key]
正确读取配置,从json或xml,但在.netcore2应用程序内。
If I port the keys
of question to appSettings.json then the call into ConfigurationManager.AppSettings
always returns null. 如果我将问题的
keys
移植到appSettings.json,那么对ConfigurationManager.AppSettings
的调用总是返回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]
The setup you are looking for is possible and the settings can be kept as-is in app.config. 您正在寻找的设置是可能的,设置可以保存在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. 我有一个.NET 4.5.2类库“MyLibrary”和一个.NET Core 2.0 Web应用程序“WebApp”,它引用了完整的.NET框架4.6.1。
MyLibrary 我的图书馆
System.Configuration
System.Configuration
的程序集引用 class Foo
which reads an appsetting with key foo
class Foo
,用键foo
读取appsetting WebApp Web应用程序
System.Configuration
System.Configuration
的程序集引用 app.config
file containing the appSettings
section. appSettings
部分的app.config
文件。 ( App.config
is by default present in a .NET Core 2.0 web application project.) App.config
存在于.NET Core 2.0 Web应用程序项目中。) Foo
, calls its GetSomething
method by which the value bar
gets assigned to the variable something
. Foo
,称其GetSomething
通过该值法bar
被分配给变量something
。 All other files and settings are the default ones. 所有其他文件和设置都是默认值。 Here below are the ones mentioned above.
以下是上面提到的那些。
.NET 4.5.2 .NET 4.5.2
Foo.cs 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 referencing full .NET framework 4.6.1. .NET Core 2.0.1引用完整的.NET框架4.6.1。
WebApp.csproj 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 App.config中
<configuration>
<runtime>
<gcServer enabled="true"/>
</runtime>
<appSettings>
<add key="foo" value="bar"/>
</appSettings>
</configuration>
Program.cs 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.