【发布时间】:2018-03-03 19:07:39
【问题描述】:
我在 NetStandard2.0 中创建了一个程序集。它使用 System.Configuration.ConfigurationManager 读取 AppSettings。我已经安装了适用于 NetStandard2.0 的 4.4.X 版本的 System.Configuration.ConfigurationManager nuget 包。
当我在控制台应用程序 (.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 开发的控制台应用程序一起使用。
请帮忙!!!
【问题讨论】:
-
Microsoft 刚刚宣布了“适用于 .NET Core 的 Windows 兼容包”。除其他外,它包含
System.Configuration.ConfigurationManager(但反过来将您的 .NET 核心应用程序变成“仅限 Windows”)。我不知道它是否已经可用。只需谷歌引号中的术语,看看你能找到什么。 -
您是否启用了 github.com/dotnet/announcements/issues/31 中所述的自动绑定重定向生成?
-
@MartinUllrich 是的,我今天试过了,但遇到了同样的异常。
-
解决了!刚刚将 System.Configuration.ConfigurationManager 4.4.0 的 nuget 添加到控制台应用程序
标签: c# .net .net-standard .net-standard-2.0