无法加载引用.Net标准库的已发布.Net 4.6.1项目中的文件或程序集'System.ComponentModel.Annotations'

Kat*_*tie 5 .net c# .net-assembly assembly-binding-redirect .net-standard

请注意:这是不是一个重复无法加载文件或程序集“System.ComponentModel.Annotations,版本= 4.1.0.0。与链接的问题不同,此问题仅发布应用程序之后发生。正如您将在下面看到的那样,我尝试了该帖子中提出的所有解决方案,但均未成功。

在WPF .Net Framework 4.6.1项目中,该问题引用了.Net Standard 2.0库,该库本身引用了System.ComponentModel.Annotations NuGet包。

该问题在以下项目中再现:https : //github.com/kaitlynbrown/DataAnnotationsError

重现该错误:

  1. 克隆上面链接的仓库
  2. 克隆上面链接的仓库
  3. 在Visual Studio中打开解决方案
  4. 在Visual Studio中,建立->发布
  5. 点击完成
  6. 安装并运行发布的应用程序

您将看到以下错误:

DataAnnotationError

我已经尝试了多种方法来解决此问题,包括:

在WPF项目的.csproj中添加以下行:

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
Run Code Online (Sandbox Code Playgroud)

在App.config中添加以下绑定重定向:

<runtime>
  <dependentAssembly>
    <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/>
  </dependentAssembly>
</runtime>
Run Code Online (Sandbox Code Playgroud)

在WPF项目中添加对System.ComponentModel.Annotations NuGet包的引用

这些都不起作用。

请注意:问题不在 Visual Studio中构建和运行。我能够做到这一点而没有错误。尝试发布应用程序并随后运行发布的应用程序时,会发生此问题。

des*_*dgc 3

Visual Studio 中存在与 ClickOnce 发布相关的错误,该错误似乎发布逻辑试图变得聪明,并“有帮助地”排除它认为是框架一部分的 DLL。当然,在混合替换完整框架功能的 .NET Standard 包时,这会导致各种问题。

参考:

我个人没有尝试过 GitHub 问题中建议的解决方法,但 System.Net.Http.dll 也有类似的问题,通过将 NuGet 包中的 dll 链接显式添加到项目文件中(添加现有 >显示所有文件 > 添加为链接)并设置“始终复制”。需要明确的是:我的意思是添加 dll 作为“内容”——而不是添加对 dll 的引用。链接的 DLL 将始终复制到发布输出。