Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make code contracts work with deserialization of data contracts?

I have written a ContractInvariantMethod for a data contract class, and everything works great on the client side, however when an object of this type is sent to my service, and the Data Contract Deserializer tries to deserialize it, code contract checking gets in the way and throws ContractException, saying invariant failed. The reason is that in the (default) constructor of the class, i set the properties to satisfy the invariant, but apparently the constructor doesn't get called when the object is being deserialized. is there a solution to this?

here is my data contract class:

[DataContract]
public class DataContractClass
{
 public DataContractClass()
 {
  this.Field1= this.Field2= -1;
 }
 [DataMember]
 public int Field1 {get; set;}
 [DataMember]
 public int Field2 {get; set;}

 [ContractInvariantMethod]
 private void Invariants()
 {
  Contract.Invariant(this.Field1== -1 || this.Field2== -1);
 }
}
like image 911
Arash Avatar asked Aug 07 '11 11:08

Arash


People also ask

What is data contract serialization?

Most important, the DataContractSerializer is used to serialize and deserialize data sent in Windows Communication Foundation (WCF) messages. Apply the DataContractAttribute attribute to classes, and the DataMemberAttribute attribute to class members to specify properties and fields that are serialized.

Which namespace is used in WCF for data serialization?

DataContractSerializer as the Default By default WCF uses the DataContractSerializer class to serialize data types.

Which class is required to configure to use the DataContractJsonSerializer?

Json namespace. If your scenario requires the DataContractJsonSerializer class, you can use it to serialize instances of a type into a JSON document and to deserialize a JSON document into an instance of a type.

What is by serialization with respect to WCF?

The process forms a sequence of bytes into a logical object; this is called an encoding process. At runtime when WCF receives the logical message, it transforms them back into corresponding . Net objects. This process is called serialization.


1 Answers

During runtime checking, invariants are checked at the end of each public method.

So when the Serializer set Property1 and Property2 not to -1 you get a contract exception because the deserializer dont use the constructor.

So use this:

public DataContractClass()
{
    SetDefaults();
}

[OnDeserializing]
private void OnDeserializing(StreamingContext context)
{
    SetDefaults();
}

private void SetDefaults()
{
    Property1 = -1;
    Property2 = -1;
}
like image 118
Skomski Avatar answered Dec 29 '22 14:12

Skomski