无法加载引用.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中添加以下行: / p>
< RestoreProjectStyle> PackageReference< / RestoreProject样式>
< AutoGenerateBindingRedirects> true< / AutoGenerateBindingRedirects>
< GenerateBindingRedirectsOutputType> true< / GenerateBindingRedirectsOutputType>
在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>
在WPF项目中添加对System.ComponentModel.Annotations NuGet包的引用
这些都不起作用。
请注意:问题是没有在Visual Studio中构建并运行。我能够做到这一点而没有错误。尝试发布应用程序并随后运行已发布的应用程序时,会发生此问题。
Visual Studio中有一个与ClickOnce相关的错误在似乎合理的发布逻辑上进行发布,并且有帮助地排除了它认为是框架一部分的DLL。当然,当混合使用.NET Standard软件包来替换完整的框架功能时,这会引起各种问题。
参考:
- .NET Standard问题并单击一次 (GitHub上的dotnet / standard)
- HttpClient库的ClickOnce部署(Microsoft Connect)
我还没有亲自尝试过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屋!