6

Trying to make a serializable class in ASP.NET vNext Class Library Project. Not able to give my class items [DataContract] or [DataMember] Attributes. As I just started exploring vNext so I am little confused. If some one can guide me whether I am doing the right thing or not. My little sample code.

using System;
using System.Runtime.Serialization;

namespace Schlouds.Business.Entities
{
    [DataContract]
    public class Student
    {
        [DataMember]
        public Guid StudentId { get; set; }

    }
}
Eilon
  • 25,582
  • 3
  • 84
  • 102
Ali
  • 73
  • 1
  • 6
  • If you want to use the DataContractSerializer you need to mark your types with the required attributes. You state you cannot (you don't give a reason why) and you show code that does. It isn't clear what the issue is here. –  Jan 26 '15 at 16:16
  • The code sample I pasted here have errors on attribute tags (DataContract and DataMember). The swirly lines of IDE wont appear hear in stack's editor. The problem I am facing in this code is that even after using namespace for serialization, VS is not able to identify both attributes and thus giving me compile time error. – Ali Jan 27 '15 at 10:16
  • You can also grab this package from nuget package store in side VS2015. Just search 'System.Runtime.Serialization' and it should find it and allow you to add it to a specific project. – IbrarMumtaz Mar 19 '16 at 17:44

3 Answers3

7

It seems you have to add "System.Runtime.Serialization" to the "frameworkAssemblies".

See example project.json file:

"net45": {
        "frameworkAssemblies": {
            "System.Runtime.Serialization": "4.0.0.0"
        },
        "dependencies": {
            "System.Reflection": "4.0.10-beta-22416",
            "System.Reflection.Extensions": "4.0.0-beta-22526",
            "System.Reflection.Primitives": "4.0.0-beta-22526",
            "System.Reflection.TypeExtensions": "4.0.0-beta-22526",
            "System.Collections": "4.0.10-beta-22526",
            "System.Collections.Specialized": "4.0.0-beta-22526",
            "System.Linq": "4.0.0-beta-22526",
            "System.Linq.Expressions": "4.0.0-beta-22526",
            "System.Linq.Queryable": "4.0.0-beta-22526",
            "System.Runtime": "4.0.20-beta-22526",
            "System.Runtime.Serialization.Primitives": "4.0.0-beta-22526",
            "System.Runtime.Serialization.Xml": "4.0.10-beta-22526",
            "System.Runtime.Serialization.Json": "4.0.0.0-beta-22526"
        }
    }
Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121
  • This works, but is a shame since it means that it's just pointing to an assembly in the GAC rather than a package. http://stackoverflow.com/q/30747965/299327 – Ryan Gates Mar 30 '16 at 16:26
3

This is the unofficial API to match the old namespace to the new ones.

http://packagesearch.azurewebsites.net/

for those attributes you need System.Runtime.Serialization.Xml 4.0.10-beta-22416 if you using core

Son_of_Sam
  • 1,913
  • 2
  • 22
  • 37
0

You can use Microsoft.AspNetCore.Mvc.Formatters.Json (or Microsoft.AspNetCore.Mvc.Formatters.Xml) package from Nuget for .net core.

Lana
  • 11
  • 1