tags:

views:

1605

answers:

3

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? :\

Confusingly yours, Graham

A: 

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.

Terry Donaghe
Yes the model is marked as both actually, as I need serialization on the client end to be performed for saving to file.
GONeale
Interesting. I got nothing at this point. Will think about it some more.
Terry Donaghe
Np. It's hard not seeing my model, but it's a pretty standard model although one would assume, OK, error model works. Client model does not work. There is incompatibilities between the models, so I am trying to cut it back as Client has another custom object, and see what that does...
GONeale
Omg. I've worked it out, a class cannot be marked as [DataContract] and [Serializable] together in WCF. The object results in empty! It seems the object is transferring just fine with [Serializable] alone which is the main attribute I need anyway. Thanks for your help.
GONeale
Awesome! Very happy you figured it out. AND I learned something new! :)
Terry Donaghe
Please leave us an answer saying how you figured it out so other people will know your solution without looking in the comments.
Terry Donaghe
A: 

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.

MattK
Thanks for the comments MattK, as you can see I worked out what it was :(
GONeale
+1  A: 

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....

GONeale
What happened with [DataMemebers], do you also change those to [Serializable]?
sebastian