rebulanyum
rebulanyum

Reputation: 527

System.Runtime.Serialization.Json nuget package error on .NetStandard

I've posted an issue on .NetStandard repository but they just didn't care for 12 days for now.. So, I wanted to try Stackoverflow. Here is the link of my issue.

I've started to use .NetCore version=1.0.0-preview2-003121 with Visual Studio 2015. My project.json file is below:

{
  "version": "1.0.0-*",
  "frameworks": {
    "netstandard2.0": {
      "dependencies": {
        "System.Runtime.Serialization.Xml": "4.3.0",
        "System.Runtime.Serialization.Json": "4.3.0"
      }
    }
  }
}

My code file is below:

using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace NetCoreTests
{
    public class Class1
    {
        public Class1()
        {
            XmlObjectSerializer ser = new DataContractJsonSerializer(typeof(int));
        }
    }
}

And this is the error I get when I try to build the solution:

CS0029  Cannot implicitly convert type 'System.Runtime.Serialization.Json.DataContractJsonSerializer' 
to 'System.Runtime.Serialization.XmlObjectSerializer'   NetCoreTests..NETStandard,Version=v2.0

Looks like DataContractJsonSerializer class in System.Runtime.Serialization.Json nuget package doesn't inherit XmlObjectSerializer class in System.Runtime.Serialization.Xml nuget package. I can see that in general there's no dependency between these 2 nuget packages; however, if you check 4 and later versions of .Net Framework DataContractJsonSerializer always inherits from XmlObjectSerializer because as far as I know every DataContractSerializer inherits from XmlObjectSerializer. Even in this repository this inheritance exists: https://github.com/dotnet/standard/blob/master/netstandard/ref/System.Runtime.Serialization.cs#L332

What can you advice for me about this situation? Am I doin something wrong?

Upvotes: 2

Views: 1009

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100581

Don't use the VS 2015 based preview tooling for .NET Core / .NET Standard to try to build for .NET Standard 2.0.

If you use VS 2017 / .NET Core SDK 2.0.0+ then you get the correct reference assemblies to build (which are pulled from the NETStandard.Library 2.0.0 NuGet package - the System.* packages are trimmed out by msbuild logic if they are referenced).

Your code sample works using a new .NET Standard 2.0 project using tooling that actually supports .NET Standard 2.0 (VS 2017, CLI 2.0.0).

Note that in .NET Standard < 2.0, the DataContractJsonSerializer did not inherit from XmlObjectSerializer - which is the one you are getting via the referenced NuGet package (since you are missing the tooling that trims out this package and adds correct .NET Standard reference assemblies).

Upvotes: 1

Related Questions