Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom object returning as "empty" from WCF?

Tags:

.net

wcf

For what reason(s) should WCF return me a "empty" instantiated object when it was clearly populated on my WCF service return before it went over the wire?

For instance a simple OperationContract method:

response.Client = new Client();
response.Client.ID = 99;
return response;

returns an "empty" Client object (on the client receiving end) and all fields are either null or zero. However just before the response, if I inspect response.Client.ID it is populated with 99?

Just to make matters worse, I have an error object and I populate as such:

response.Errors.Add(new CodedError(Errors.ErrorCodes.LOGIN_AUTHENTICATION_ERROR));

However I CAN see the Error list on the receiving end with this?

like image 478
GONeale Avatar asked Jan 29 '09 22:01

GONeale


3 Answers

If anyone encounters this problem, I have found the fix. Due to business requirements I had marked my custom class with both [Serializable] and [DataContract], this appears to be illegal possibly as of .NET 3.5 SP1?

I have a friend who is sending WCF objects with both these attributes pre .NET 3.5 SP1 and it is working fine. Interesting.

FYI, I simply used [Serializable] only and it is sending through my object graph correctly. I needed this for xml serialization down the track.

This was a painful issue but glad it is now finally functioning....

like image 128
GONeale Avatar answered Sep 18 '22 20:09

GONeale


Is your object marked as [Serializable] or is it a [DataContract]? You need to mark your object as one or the other.

WCF only knows how to pass primitives or serializable objects across the wire.

like image 39
Tad Donaghe Avatar answered Sep 17 '22 20:09

Tad Donaghe


Is the client proxy up to date? I've seen it happen when the contract changes and the client is not updated to reflect the change.

like image 36
Kwal Avatar answered Sep 17 '22 20:09

Kwal