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.