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

回眸只為那壹抹淺笑 提交于 2019-12-22 06:27:12

问题


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:

  1. Clone the repo linked above
  2. Clone the repo linked above
  3. Open the solution in visual studio
  4. In Visual Studio, Build -> Publish
  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:

<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.


回答1:


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.



来源:https://stackoverflow.com/questions/47803812/could-not-load-file-or-assembly-system-componentmodel-annotations-in-published

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!