我在 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/