4

I just took out a brand new copy of Visual Studio 2015 on a brand new copy of Windows 10. I tried to create a simple Portable Class Library (PCL) and tried to add a simple data contract:

namespace ClassLibrary1
{
    using System.Runtime.Serialization;

    [DataContract]
    public class Class1
    {
    }
}

And the compiler tells me:

The type or namespace name 'DataContract' could not be found (are you missing a using directive or an assembly reference. It appears the namespace System.Runtime.Serialization is missing when .NET 4.6 is selected as the target.

There appears to be no serialization available when .NET Framework 4.6 is selected for the Targets. If I drop back to .NET 4.5.1, then the same code compiles (and runs in a more complicated project). What's going on here? Is .NET 4.6 not ready for Prime Time in Visual Studio? Anyone else run into this?

4
  • If you want to use .NET 4.6 then there is no point in creating a PCL project, just use the Class Library project template instead. The PCL project helps you avoid using features that are not available on one of the targets you selected. Like data contracts. Aug 19, 2015 at 12:35
  • Do you understand why people use the PCL? I develop Windows 8.1 products as well as WPF. How am I supposed to get my Windows 8.1 apps to work with .NET 4.6?
    – Quark Soup
    Aug 19, 2015 at 14:16
  • Hmm, surely it is much more important that you understand it. Window 8.1 apps target .NETCore, a much smaller version of the CLR. Suitable for mobile devices. It only ever gets smaller by dropping features. That programmers are never happy that their can't-live-without-out feature got dropped is a given. Aug 19, 2015 at 14:28
  • @HansPassant: Absolutely right, but unfortunately, PCLs are one of the only ways to share classes between e.g. WebAPI and UWP apps, which means this particular omission is a bit unfortunate. Oct 16, 2015 at 17:51

1 Answer 1

7

Had the same problem here, and it appears the solution is to add the relevant NuGet package(s) to the project that contains the functionality that has been moved out of Core. Specifically, you need the Serialization Primitives, but I've included the project.json file below that is probably closer to what you want in terms of actual configuration (dependencies, etc.)

This site also has a "search engine" of sorts for .NET 5 packages, which is basically what you're doing here.

{
"supports": {
    "net46.app": {},
    "uwp.10.0.app": {},
    "dnxcore50.app": {}
  },
  "dependencies": {
    "Microsoft.NETCore": "5.0.0",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.0",
    "System.Collections": "4.0.10",
    "System.Collections.Specialized": "4.0.0",
    "System.Linq": "4.0.0",
    "System.Linq.Expressions": "4.0.10",
    "System.Linq.Queryable": "4.0.0",
    "System.Net.Requests": "4.0.10",
    "System.Runtime": "4.0.20",
    "System.Runtime.Serialization.Primitives": "4.0.10",
    "System.Runtime.Serialization.Json": "4.0.0",
    "System.Runtime.Serialization.Xml": "4.0.10"
  },
  "frameworks": {
    "dotnet": {
      "imports": "portable-net452+win81"
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.