Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portable Class Library has no DataContract or Serialization functions with .NET 4.6

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?

like image 354
Quarkly Avatar asked Mar 15 '23 01:03

Quarkly


1 Answers

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"
    }
  }
}
like image 50
OperatorOverload Avatar answered Apr 25 '23 22:04

OperatorOverload