[英]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:要重现错误:
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.