0

I am facing a strange problem in Wcf. When I am passing a object with values in wcf and debugging that object in wcf service, it is showing null values in all int and datetime properties but string values is showing in string type properties. I am using wcf server side.

I have checked all configuration and service but didn't find any problem. Please help if anyone knows about it .

Thanks in Advance.

Deepak
  • 7,507
  • 3
  • 24
  • 26
  • 2
    You are probably forgetting to put `[DataContract]` annotation on your data contract class or/and `[DataMember]` annotation on your fields in your data contract. – Tolga Evcimen Dec 27 '13 at 07:02
  • Previously it was running fine without DataContract and DataMember. – Deepak Dec 27 '13 at 07:05
  • In case you were using your wcf service as a class reference in your project that migth have been the case, but otherwise I don't see any rational case that a wcf service can accept parameters without those annotations. – Tolga Evcimen Dec 27 '13 at 07:09
  • Yes, you are right, We are using Wcf a reference in project and we are using approx 300 DTOs without DataMember and till now it was working fine but in somedays we are facing this problem. Is it any solution for it . Why it is happening? – Deepak Dec 27 '13 at 07:24
  • Of course there is a solution for it, as @No One suggests, decorate all your data contracts with necessary annotations. – Tolga Evcimen Dec 27 '13 at 07:40
  • When something works for a while and then stops working, I usually suspect that a) something changed in the configuration, b) someone changed some code somewhere or c) server(s) were patched and one of the patches broke your existing application. I'd check to make sure no one has changed any of the code first and then the configuration files second. – Tim Dec 27 '13 at 08:19
  • 1
    Also, you do **not** have to use the `[DataContract]` attributes as of .NET 3.5 SP1 - see this [answer](http://stackoverflow.com/a/4836803/745969). – Tim Dec 27 '13 at 23:10

1 Answers1

2

In order to pass an object from service to consumer. you need to decorate the class with [DataContract] attribute, and all the members that need to be passed to consumer with [DataMember] attribute. Read MSDN

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. A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged

Example:

[DataContract]
public class SomeClass
{
    [DataMember]
    public int someMember;
}
Ehsan
  • 31,833
  • 6
  • 56
  • 65