简体   繁体   English

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

[英]Could not load file or assembly 'System.ComponentModel.Annotations' in published .Net 4.6.1 project referencing .Net Standard library

PLEASE NOTE : This is not a duplicate of Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.1.0.0 .请注意:这不是无法加载文件或程序集 'System.ComponentModel.Annotations, Version=4.1.0.0的副本。 Unlike the issue linked, this issue occurs only after publishing the application.与链接的问题不同,此问题仅发布应用程序发生。 As you will see below, I have tried every solution proposed in that post with no success.正如您将在下面看到的,我尝试了该帖子中提出的所有解决方案,但均未成功。

This issue occurs in a WPF .Net Framework 4.6.1 project which references a .Net Standard 2.0 library which itself references the System.ComponentModel.Annotations NuGet package.此问题发生在 WPF .Net Framework 4.6.1 项目中,该项目引用了 .Net Standard 2.0 库,该库本身引用了 System.ComponentModel.Annotations NuGet 包。

The issue is reproduced in the following project: https://github.com/kaitlynbrown/DataAnnotationsError该问题在以下项目中重现: https : //github.com/kaitlynbrown/DataAnnotationsError

To reproduce the error:要重现错误:

  1. Clone the repo linked above克隆上面链接的 repo
  2. Clone the repo linked above克隆上面链接的 repo
  3. Open the solution in visual studio在visual studio中打开解决方案
  4. In Visual Studio, Build -> Publish在 Visual Studio 中,构建 -> 发布
  5. Click Finish单击完成
  6. Install and run the published application安装并运行已发布的应用程序

You will see the following error:您将看到以下错误:

数据注释错误

I have tried a number of things to resolve this issue, including:我已经尝试了很多方法来解决这个问题,包括:

Adding the following lines to the WPF project's .csproj:将以下行添加到 WPF 项目的 .csproj:

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

Adding the following binding redirects in App.config:在 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>

Adding a reference to the System.ComponentModel.Annotations NuGet package in the WPF project在 WPF 项目中添加对 System.ComponentModel.Annotations NuGet 包的引用

None of these things has worked.这些事情都没有奏效。

PLEASE NOTE: The problem is not building and running within Visual Studio.请注意:问题不是在 Visual Studio 中构建和运行。 I am able to do that without errors.我能够做到这一点而不会出错。 The problem occurs when attempting to publish the application and subsequently running the published app.尝试发布应用程序并随后运行已发布的应用程序时会出现此问题。

There's a bug in Visual Studio related to ClickOnce publishing where it seems like the publishing logic is trying to be clever and "helpfully" excludes DLLs that it thinks are part of the framework. Visual Studio 中有一个与 ClickOnce 发布相关的错误,它似乎发布逻辑试图变得聪明,并且“有帮助地”排除了它认为是框架一部分的 DLL。 Of course, this causes all sorts of problems when mixing .NET Standard packages that replace full framework functionality.当然,在混合替换完整框架功能的 .NET Standard 包时,这会导致各种问题。

Reference:参考:

I haven't personally tried the workaround suggested in the GitHub issue, but had a similar issue with System.Net.Http.dll that was resolved by explicitly adding a link to the dll from the NuGet package to the project file (Add Existing > show all files > Add As Link) and setting "Copy Always."我还没有亲自尝试过 GitHub 问题中建议的解决方法,但是 System.Net.Http.dll 有一个类似的问题,该问题通过显式地将指向 NuGet 包中的 dll 的链接添加到项目文件(添加现有 >显示所有文件 > 添加为链接)并设置“始终复制”。 To be clear: I mean adding the dll as "content" - not adding a reference to the dll.需要明确的是:我的意思是将 dll 添加为“内容”——而不是添加对 dll 的引用。 The linked DLL will always be copied to the publish output.链接的 DLL 将始终复制到发布输出。

I didn't run into error after running published version of app.运行已发布版本的应用程序后,我没有遇到错误。 在此处输入图片说明

But I ran into this problem today in an application which I am making.但是我今天在我正在制作的应用程序中遇到了这个问题。 Problem was that debugging of application run fine but unit tests failed with this error.问题是应用程序的调试运行良好,但单元测试因此错误而失败。 I fixed this problem by increasing .NET framework version from 4.7 to 4.7.2 and setting AutoGenerateBindingRedirects, GenerateBindingRedirectsOutputType to true as you mentioned.我通过将 .NET 框架版本从 4.7 增加到 4.7.2 并将 AutoGenerateBindingRedirects、GenerateBindingRedirectsOutputType 设置为 true 来解决这个问题,正如你提到的。 Tests were working fine with 4.8 too.测试在 4.8 上也运行良好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在设计器中加载文件或程序集 System.Componentmodel.Annotations 4.2.0.0 - Could not load file or assembly System.Componentmodel.Annotations 4.2.0.0 in designer 无法加载文件或程序集&#39;System.ComponentModel.Annotations - Could not load file or assembly 'System.ComponentModel.Annotations 无法加载文件或程序集“System.ComponentModel.Annotations,版本 = 4.2.0.0, - Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0, Azure Function:System.Private.CoreLib:无法加载文件或程序集“System.ComponentModel.Annotations...” - Azure Function: System.Private.CoreLib: Could not load file or assembly 'System.ComponentModel.Annotations…' JsonConvert.DeserializeObject 抛出无法加载文件或程序集'System.ComponentModel.Annotations,版本 = 4.3.0.0,文化 - JsonConvert.DeserializeObject throws Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.3.0.0, Cultur .NET 标准库 - 无法加载文件或程序集“System.Runtime.Caching” - .NET Standard library - Could not load file or assembly 'System.Runtime.Caching' 无法加载文件或程序集 System.ComponentModel.Annotations。 该系统找不到指定的文件 - Could not load file or assembly System.ComponentModel.Annotations. The system cannot find the file specified 如何修复.net标准2.0项目中的“无法加载文件或程序集System.IO.Packaging,版本= 4.0.3.0” - How to fix 'Could not load file or assembly System.IO.Packaging, Version=4.0.3.0' in a .net standard 2.0 project 引用.NET Standard 2.0库的.NET 4.6.1项目-VSTS不会构建 - .NET 4.6.1 project referencing a .NET Standard 2.0 library - VSTS wont build 无法加载文件或程序集System.Xml.XPath - .Net 4.6.2引用.Net Standard 1.3 - Impossible to load file or assembly System.Xml.XPath - .Net 4.6.2 referencing .Net Standard 1.3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM