c# - 无法在 .NET FX4.6 上的 .NET Standard2.0 库中使用 System.Configuration.Configuration 管理器

标签 c# .net .net-standard .net-standard-2.0

我在 NetStandard2.0 中创建了一个程序集。它使用 System.Configuration.ConfigurationManager 读取 AppSettings。我已经安装了 System.Configuration.ConfigurationManager 的 nuget 包,版本为 4.4.X,适用于 NetStandard2.0

当我在控制台应用程序 (.Net Core) 中引用此程序集时,它正在正确读取 AppSettings,但是当我在旧的 .NetFramework(4.6.X) 控制台应用程序中引用此程序集时,它无法正常工作并引发异常。

请看下面的代码。

程序集 1:NetStandard 2.0

Nuget:System.Configuration.ConfigurationManager 4.4.0

using System.Configuration;

namespace Bootstrapper.Lib
{
   public class Bootstrapper
   {
     public Bootstrapper()
     {

     }

     public void LoadAppSettings()
     {
         string serachPattern= 
         ConfigurationManager.AppSettings["AssemblySearchPattern"];
     }
  }

}

控制台应用:NetFx 4.6.X

using System;
using Bootstrapper.Lib;
namespace Bootstrapper.Console
{
  class Program
  {
    static void Main(string[] args)
    {
        new Bootstrapper().LoadAppSettings();
    }
  }
}

运行后异常:

'Could not load file or assembly 'System.Configuration.ConfigurationManager, 
 Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one 
 of its dependencies. The system cannot find the file specified.'

它将与使用 .NetCore 开发的控制台应用程序一起使用。

请帮忙!!!

最佳答案

正如@kiran 在评论中提到的,您可以通过运行来解决此问题:

Install-Package System.Configuration.ConfigurationManager

在 NuGet 包管理器中

关于c# - 无法在 .NET FX4.6 上的 .NET Standard2.0 库中使用 System.Configuration.Configuration 管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46360716/

相关文章:

C# NET 5.0 源代码生成器基本示例不生成输出

c# - 使用通过 .NET Core 和 Xamarin 从 IL 编译的程序集

c# - 替代 .net 标准库上的 SecureString?

c# - 基本泛型接口(interface)上的扩展方法

C#MVC : Cannot Debug in IIS Express

c# - 试图了解C#方法的签名

c# - 类型为 'LinkButton' 的控件 xxx 必须放置在带有 runat=server 的表单标记内

c# - 在 Xamarin Forms C# 中发送 HTTP Post 请求

c# - 使用 HttpClient 从 Web API 反序列化 Json

F# .NET 标准库项目