Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datacontract exception. Cannot be serialized

Tags:

c#

.net

wcf

I have the following WCF DataContract:

[DataContract] public class Occupant {     private string _Name;     private string _Email;     private string _Organization;     private string _Badge;      public Occupant(string name, string badge, string organization)     {         Name = name;         Badge = badge;         Organization = organization;     }      public Occupant(string name, string badge)     {         Value = name;         Key = badge;     }      [DataMember]     public string Key     {         get { return _Name; }         set { _Name = value; }     }      [DataMember]     public string Value     {         get { return _Badge; }         set { _Badge = value; }     }      [DataMember]     public string Name     {         get { return _Name; }         set { _Name = value; }     }      [DataMember]     public string Email     {         get { return _Email; }         set { _Email = value; }     }      [DataMember]     public string Organization     {         get { return _Organization; }         set { _Organization = value; }     }      [DataMember]     public string Badge     {         get { return _Badge; }         set { _Badge = value; }     } } 

When I try to access this service via web browser (it is hosted on IIS), I am getting this error:

System.Runtime.Serialization.InvalidDataContractException: Type 'MyNamespace.Occupant' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute.

One of my methods is returning a List of type Occupant. Would this be causing it?

like image 269
Justin Avatar asked Apr 09 '12 17:04

Justin


People also ask

What is Datacontract serialization?

DataContractSerializer(Type, IEnumerable<Type>) Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type, and a collection of known types that may be present in the object graph.

Is Datacontract mandatory in WCF?

No, the DataContractAttribute is not required - WCF will infer serialization rules.

What is Datacontract and Datamember in WCF?

A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.

Why Datacontract is used in WCF?

A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts.


2 Answers

Because you have provided one or more initializing constructors, you will also need to add a parameterless (default) constructor.

i.e. You need to add:

[DataContract] public class Occupant {     // *** Needed only for Serialization     public Occupant() {}     ... 

This is because the default constructor disappears when you add an explicit constructor.

[The issue isn't with the method returning List<Occupant>, since methods aren't serialized).]

like image 142
StuartLC Avatar answered Sep 29 '22 13:09

StuartLC


Try adding an empty constructor. Often times that will set off the serializer.

like image 32
Rob Rodi Avatar answered Sep 29 '22 14:09

Rob Rodi