Property in WCF service with some initial value to use in client side
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, 开发者_如何学Pythonit always return '0' not '50'.
How do i fix this issue?
I have tried [DataMember(EmitDefaultValue = false)] but not working... :(
Thank you,
Riju
I faced the same issue; after doing a lot of tries on the same I ended up finding out that the issue is lying with the DataType
.
I had a data type Int32
as per database, and properties used were having types String
, it does not give any compilation or execution issue as int
can be serialized by default to string
.
But I was seeing empty value every time, I changed the property Type to Int32
and it worked like a charm. Give it a shot, confirm your data types as they show correct data on compiler, till the first service call, but as soon as you cross the service you will see empty data.
So if I tell the solution in one line, if the Datatypes are not the same; it may return empty values.
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.
精彩评论