1

Background:

using Visual Studio 2017 v15.4 (no ReSharper or any weird extensions)

Repro solution includes 2 projects:

  1. WPF project: .NET 4.6.1, using NuGet PackageReference format
  2. Class library: NET Standard 2.0

Both projects have the System.ComponentModel.Annotations NuGet package installed.

Problem

After upgrading my WPF project's NuGet format from the old packages.config to the new PackageReference, I started experiencing design-time errors: enter image description here

Or sometimes the harsher flavor: enter image description here

To start with, the following ViewModel code works fine:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public class MainWindowViewModel
{
    public ValClass MyProp => new ValClass();
}

public class ValClass : IValidatableObject
{
    //Implementation stuff
}

However, if I then define an identical class in my NET Standard project, and include that as a type in my ViewModel, it produces the error.

Comparing an F12 on the IValidatableObject of these 2 classes:

The WPF version:

#region Assembly System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.1\System.ComponentModel.DataAnnotations.dll
#endregion

The NET Standard 2.0 version:

#region Assembly System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:\Users\a0110\.nuget\packages\system.componentmodel.annotations\4.4.0\ref\netstandard2.0\System.ComponentModel.Annotations.dll
#endregion

It's clear that the same type and namespace are defined in assemblies with different names--not just assembly versions.

How can I get the XAML Designer to successfully locate whatever assembly is required to use IValidatableObject?

Things I have Tried

  • Installing VS v15.5 preview and targeting the WPF project to .NET 4.7 (also 15.5 preview 2 and 3, .NET 4.7.1)
  • Adding the old System.ComponentModel.DataAnnotations reference to the WPF project (with and without the NuGet version)
  • reverting back to packages.config format (this didn't actually succeed; it appears that I'm stuck with PackageReference!)
  • grabbing a copy of the System.ComponentModel.Annotations.dll from the project's output folder and referencing it directly (VS would not let me do this)
  • adding a bindingRedirect to my App.config
  • adding <DependsOnNETStandard>True</DependsOnNETStandard> to the .csproj
  • adding <DependsOnNETStandard>netstandard2.0</DependsOnNETStandard> to .csproj
  • adding <_HasReferenceToSystemRuntime>true</_HasReferenceToSystemRuntime> to .csproj
  • adding the NETStandard.Library NuGet package to the WPF project

Update

For anyone else who is plagued by this issue, I have found a workaround:

  1. Locate the System.ComponentModel.Annotations.dll on your hard drive that your NET Standard project uses for IValidatableObject. Copy it to a location within your project.
  2. Uninstall the System.ComponentModel.Annotations NuGet package from your solution
  3. Give your NET Standard project a direct reference to DLL you copied
  4. Give your WPF project a reference to the standard assembly System.ComponentModel.DataAnnotations
  5. Upgrade your WPF project to target .NET 4.7.1
  6. Compile. Your WPF design-time should be free from this error.

.

BCA
  • 7,776
  • 3
  • 38
  • 53
  • I have got a similar problem already for some weeks that I have been unable to solve. in an WCF service using EF Core 2.0 I used a Netstandard 2.0 library targeting Net 4.6.1 instead of a normal class library (it saves a few hundred megabytes). The library compiles normally but whenever I use the WCF-service from an application it always complains with the errormessage: Cannot find System.ComponentModel.Annotations version 4.2.0.0 – JRB Nov 07 '17 at 19:13
  • Maybe you should rephrase the title of this question. – JRB Nov 07 '17 at 19:22
  • @JRB there seems to be a lot of workarounds mentioned at https://github.com/dotnet/standard/issues , but they all pertain to runtime issues. My issue is strictly a design-time issue. – BCA Nov 07 '17 at 19:28
  • thank you for the link. I came across one remark by JBE2277 of the 21th august 2017. That gave my an idea. Manually copy the .Netstandard 2.0 version of System.ComponentModel.Annotations timestamped 19th of july 2017 10:01 into the Setup folder of the WCF service. This solves my problem. – JRB Nov 07 '17 at 20:26

0 Answers0