从 .NET Standard 2.0 项目添加对程序集的引用会导致错误

Adding reference to an assembly from .NET Standard 2.0 project causses error

我有一个 .NET Standard 2.0 class 库,我想添加对 System.DirectoryServices 的引用。一切看起来都很好,但在运行时我收到了这个错误:

Cannot load a reference assembly for execution

详细描述是这样的:

Could not load file or assembly 'System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058)

我的项目“p1StandarClasese”是 .NetStandar 版本。 2.0,自动添加对 DLL 的引用“C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.8\System.DirectoryServices.dll”

引用是由 visual studio 编辑器自动添加的,VS 检测到我编写的代码需要 Assembly 并要求我添加,所以我答应了...并添加了引用

我真的不明白,我猜一个 .NET Standard 2.0 不能引用那个版本的 .NET 程序集? 如果是这种情况 - 有什么建议吗?我如何使用该 Active Directory 程序集?我需要它来进行身份验证。有解决这个问题的金块吗?

谢谢你的帮助

这是由于依赖项目的版本低于请求的版本,或者引用了错误的DLL。

查看兼容性图表以确认: https://docs.microsoft.com/en-us/dotnet/standard/net-standard

根据你的问题,我假设你使用的是 .Net Standard v2.0 class 库 并在您的项目定义中引用 System.DirectoryServices 的 4.0.0 版。

如果这不正确,您能否提供解决方案中每个项目的 .Net 版本以及引用发生的位置?错误消息应该有额外的上下文来确定你是如何引用的。

您始终可以删除引用并使用 NuGet 重做引用以确保生成正确的程序包。