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

前端之家收集整理的这篇文章主要介绍了asp.net – 引用依赖于ConfigurationManager的.net core 2中的.net框架程序集前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们有一些遗留的4.5.2类库,它们共同使用ConfigurationManager.AppSettings [key]

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

如果我将问题的键移植到appSettings.json,那么对ConfigurationManager.AppSettings的调用总是返回null.

一个例子是:

  1. {
  2. "Logging": {
  3. "IncludeScopes": false,"Debug": {
  4. "LogLevel": {
  5. "Default": "Warning"
  6. }
  7. },"Console": {
  8. "LogLevel": {
  9. "Default": "Warning"
  10. }
  11. }
  12. },"appSettings": {"test": "bo"},"test": "hi"
  13. }

然后:

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

显示

  1. ["value1",null,null]

解决方法

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

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

我的图书馆

>具有System.Configuration的程序集引用
>有一个类Foo,它用键foo读取appsetting

Web应用程序

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

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

MyLibrary项目

.NET 4.5.2

Foo.cs

  1. using System;
  2. using System.Configuration;
  3.  
  4. namespace PFX
  5. {
  6. public class Foo
  7. {
  8. public String GetSomething()
  9. {
  10. String r = ConfigurationManager.AppSettings["foo"];
  11. return r;
  12. }
  13. }
  14. }

WebApp项目

.NET Core 2.0.1引用完整的.NET框架4.6.1.

WebApp.csproj

  1. <Project Sdk="Microsoft.NET.Sdk.Web">
  2. <PropertyGroup>
  3. <TargetFramework>net461</TargetFramework>
  4. </PropertyGroup>
  5.  
  6. <ItemGroup>
  7. <PackageReference Include="Microsoft.AspNetCore" Version="2.0.3" />
  8. <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.4" />
  9. <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.4" PrivateAssets="All" />
  10. <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.3" />
  11. <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.3" />
  12. </ItemGroup>
  13.  
  14. <ItemGroup>
  15. <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
  16. </ItemGroup>
  17.  
  18. <ItemGroup>
  19. <ProjectReference Include="..\MyLibrary\MyLibrary.csproj" />
  20. </ItemGroup>
  21.  
  22. <ItemGroup>
  23. <Reference Include="System.Configuration" />
  24. </ItemGroup>
  25. </Project>

App.config中

  1. <configuration>
  2. <runtime>
  3. <gcServer enabled="true"/>
  4. </runtime>
  5.  
  6. <appSettings>
  7. <add key="foo" value="bar"/>
  8. </appSettings>
  9. </configuration>

Program.cs中

  1. using System;
  2. using Microsoft.AspNetCore;
  3. using Microsoft.AspNetCore.Hosting;
  4.  
  5. namespace WebApp
  6. {
  7. public class Program
  8. {
  9. public static void Main(string[] args)
  10. {
  11. PFX.Foo foo = new PFX.Foo();
  12. String someting = foo.GetSomething(); // bar
  13.  
  14. BuildWebHost(args).Run();
  15. }
  16.  
  17. public static IWebHost BuildWebHost(String[] args) =>
  18. WebHost.CreateDefaultBuilder(args)
  19. .UseStartup<Startup>()
  20. .Build();
  21. }
  22. }

猜你在找的.NET Core相关文章