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);
}
}