5
votes

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:

DataAnnotationError

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.

2
In the bin folder of your project is there any dlls from other projects? If so put in publish folder. Also try building with x86 option which is compatible with any PC. - jdweng
In my actual project, System.ComponentModel.Annotations is the only NuGet package whose dll is not copied over to the publish folder. It does appear in the bin folder (along with the dlls for my other projects and all NuGet packages references), and all of the other dlls seem to get copied to the publish folder except for this one. - Katie
Open the .proj file with Notepad and check if there are any special instructions to copy working dlls. - jdweng
Only the dll I added which drives the hardware I am connecting to. The NuGet packages and their dependencies all get copied automatically (as they should) except for System.ComponentModel.Annotations, for some reason - Katie
I wanted to find out where those instructions were to copy the dlls. It must either be in the current project or the 2.0 library. It is possible the 2.0 project didn't include the dll in the instructions so that is why it didn't copy. Also older dlls aren't recognized by Net Library and will not get copies. I have similar issue with an older dll that Net doesn't recognized. To use dll I have a c++ wrapper dll that I call from c#. I have to move the original script manually to bin folder to compile. I never bother to come up with instructions to move this dll. - jdweng

2 Answers

3
votes

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:

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.

0
votes

I didn't run into error after running published version of app. enter image description here

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. Tests were working fine with 4.8 too.