5

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.

Katie
  • 1,498
  • 1
  • 15
  • 33
  • 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 Dec 14 '17 at 00:41
  • 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 Dec 14 '17 at 01:44
  • Open the .proj file with Notepad and check if there are any special instructions to copy working dlls. – jdweng Dec 14 '17 at 09:55
  • 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 Dec 14 '17 at 17:15
  • 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 Dec 14 '17 at 17:23
  • It's not an older dll. The NuGet package for `System.ComponentModel.Annotations` was literally just updated yesterday. Again, for NuGet packages, you do not typically have to add explicit instructions to copy the dll. It happens automatically with every other NuGet package so why wouldn't it with this one? – Katie Dec 14 '17 at 21:31
  • Did you look at the project files with notepad to find answer? – jdweng Dec 14 '17 at 22:38
  • I already did and gave you an answer above. The only dll being explicitly copied in the .csproj is the driver I added for the hardware I am connecting to. The only other thing that is similar is the many `PublishFile Include` tags. These are also automatically generated and UI representing them can be viewed in Project -> Properties -> Publish -> Application Files. I have every single thing listed there set to Include. Unlike other NuGet package assemblies, `System.ComponentModel.Annotations` is not listed. I tried manually adding an entry for it, but that did not seem to do anything. – Katie Dec 14 '17 at 23:44
  • See also : https://learn.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package and https://learn.microsoft.com/en-us/nuget/create-packages/publish-a-package and https://learn.microsoft.com/en-us/nuget/create-packages/creating-a-package – jdweng Dec 15 '17 at 04:41

2 Answers2

3

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.

desmondgc
  • 501
  • 2
  • 9
  • 1
    The workarounds in the links you provided did not work, but linking the nuget dll does seem to work as a workaround for now. Unfortunately, I'm not sure of how I can do this in a way that will ensure the dll is updated when the nuget package is updated, *and* be able to run the build on other machines which might store their nuget packages in different locations – Katie Dec 19 '17 at 19:20
0

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.