Question

I have created a VS2013 solution that contains multiple projects. One of those projects is a portable class library targeting .NET 4.5+, Silverlight 5, Windows Store 8+ and Windows Phone 8 and this project contains a reference to an OData service in an accompanying web application.

The portable class library references Microsoft.Data.Services.Client.Portable (among others), added via nuget (WCF Data Services Client).

I have then added the portable class library to a WPF (.NET 4.5) application, a Windows Store application and a Windows Phone application and written some very basic test code to access the service. This works fine in Windows Store and Windows Phone.

The WPF application however will not compile. The errors are...

  • The type 'System.Data.Services.Client.DataServiceContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.Data.Services.Client.Portable, Version=5.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
  • The type 'System.Data.Services.Client.DataServiceQuery`1' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.Data.Services.Client.Portable, Version=5.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

The thing is, I also added the WCF Data Services Client to the WPF application via nuget, but it does not add a reference to the portable library (packages\Microsoft.Data.Services.Client.5.6.0\lib\portable-net45+sl5+wp8+win8\Microsoft.Data.Services.Client.Portable.dll), instead adding a reference to the full framework 4.0 version (packages\Microsoft.Data.Services.Client.5.6.0\lib\net40\Microsoft.Data.Services.Client.dll) - which in turn is causing the error I am seeing.

Is there something I am doing wrong here or is this an error in the installation package? Note that if I delete the offending reference and add the alternative reference manually then all is well so though I have a work-around I would still like to know if I am the cause of the error.

Was it helpful?

Solution

Looking at the NuGet source code this behaviour seems to be by design.

The WCF Data Services Client contains assemblies for the following frameworks:

  • .NET Framework, v4.0
  • .NET Portable, net45, sl5, wp8, win8
  • Silverlight, v4.0

Installing this NuGet package into a project that targets .NET 4.5 will result in the .NET 4.0 assembly from the NuGet package being referenced. NuGet considers the more specific .NET framework to be a better match for your WPF project which targets .NET 4.5.

There is a comment in the NuGet source code about this when it looks for the best matching assembly in the NuGet package.

// Let's say a package has two framework folders: 'net40' and 'portable-net45+wp8'.
// The package is installed into a net45 project. We want to pick the 'net40' folder, even though
// the 'net45' in portable folder has a matching version with the project's framework.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top