无法在引用 .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
问题描述
请注意:这不是重复
我尝试了很多方法来解决这个问题,包括:
将以下行添加到 WPF 项目的 .csproj:
PackageReference <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects><GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
在 App.config 中添加以下绑定重定向:
<从属程序集><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></运行时>
在 WPF 项目中添加对 System.ComponentModel.Annotations NuGet 包的引用
这些都没有奏效.
请注意:问题不是在 Visual Studio 中构建和运行.我能够做到这一点而不会出错.尝试发布应用程序并随后运行已发布的应用程序时会出现此问题.
Visual Studio 中存在一个与 ClickOnce 发布相关的错误,它似乎发布逻辑试图变得聪明并且有帮助地"排除了它认为是其中一部分的 DLL框架的.当然,在混合替换完整框架功能的 .NET Standard 包时,这会导致各种问题.
参考:
- .NET Standard 的问题和点击一次(GitHub 上的 dotnet/standard)
- HttpClient 库的 ClickOnce 部署(微软连接)
我没有亲自尝试过 GitHub 问题中建议的解决方法,但是 System.Net.Http.dll 有一个类似的问题,通过显式地将指向 NuGet 包中的 dll 的链接添加到项目文件(添加现有 > 显示所有文件 > 添加为链接)并设置始终复制".需要明确的是:我的意思是将 dll 添加为内容"——而不是添加对 dll 的引用.链接的 DLL 将始终复制到发布输出.
PLEASE NOTE: This is not a duplicate of Could not load file or assembly '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.
The issue is reproduced in the following project: https://github.com/kaitlynbrown/DataAnnotationsError
To reproduce the error:
- Clone the repo linked above
- Clone the repo linked above
- Open the solution in visual studio
- In Visual Studio, Build -> Publish
- Click Finish
- 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:
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
Adding the following binding redirects in 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
None of these things has worked.
PLEASE NOTE: The problem is not building and running within 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. Of course, this causes all sorts of problems when mixing .NET Standard packages that replace full framework functionality.
Reference:
- Issues with .NET Standard and Click Once (dotnet/standard on GitHub)
- ClickOnce Deployment of HttpClient Libraries (Microsoft Connect)
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." To be clear: I mean adding the dll as "content" - not adding a reference to the dll. The linked DLL will always be copied to the publish output.
这篇关于无法在引用 .Net 标准库的已发布 .Net 4.6.1 项目中加载文件或程序集“System.ComponentModel.Annotations"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!