tags:

views:

21

answers:

1

Hi,

I have a property in WCF service class with a initial value, as shown below

 private int mFieldLength_Name = 50;
 [DataMember]
 public int FieldLength_Name
 {
  get { return mFieldLength_Name; }
  private set { mFieldLength_Name = value; }
 }

However when i do call in client side, it always return '0' not '50'.

How do i fix this issue?

I have tried [DataMember(EmitDefaultValue = false)] but not working... :(

Thank you,

Riju

A: 

Are you using the same assembly of DataContracts in the client as you are on the server? If you're generating your own contracts for use in the client it will have no knowledge of this initialization because that is a behavior that cannot be translated over the wire.

When you choose a "non-natural" default for a type (i.e. 50 instead of 0 for an integer) you must apply the DefaultValueAttribute to the data member in order for the DataContractSerializer to realize that the value is the default and not emit it.

Drew Marsh
Hi Drew,Sorry for such along delay in reply...I have tried DefaultValueAttribute in those property still not able get "50" as default value.
Riju
You also need to make sure you actually initialize your property (or backing field) to 50 in the data contract class itself. This is because WCF will never even set the property because there will be no element in the XML to even parse and .NET will obviously initialize it to 0.
Drew Marsh