GONeale
GONeale

Reputation: 26494

Custom object returning as "empty" from 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?

Upvotes: 8

Views: 8368

Answers (5)

Romesh D. Niriella
Romesh D. Niriella

Reputation: 480

Might be too late now, But there's an easy fix for this particular problem. At least someone will find this useful.

I have 2 projects with different namespaces.

  1. MyProject.Business.Entities (Marked with [DataContract])
  2. MyProject.Client.Entities (Client side entities to represent business objects)

Use the AssemblyInfo.cs and add following line to each Business and Client projects.

[assembly: ContractNamespace("http://www.tempuri.org/MyProject",
                              ClrNamespace = "MyProject.Business.Entities")]

[assembly: ContractNamespace("http://www.tempuri.org/MyProject",
                              ClrNamespace = "MyProject.Client.Entities")]

Alternatively, you can do this also.

[DataContract (Namespace = "http://www.tempuri.org/MyProject")]
public class Account
{}

Upvotes: 2

Andy Cronk
Andy Cronk

Reputation: 11

I had the same issue. I had changed the namespace on the server-side objects and although I had updated the Service Reference configuration, looking closer the namespace was the old namespace, so whilst fiddler confirmed the data was being transferred across the wire correctly, the data wasn't passed to my client object. Deleting and re-adding the service reference did the trick

Upvotes: 1

GONeale
GONeale

Reputation: 26494

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

Upvotes: 8

Kwal
Kwal

Reputation: 1531

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.

Upvotes: 2

Tad Donaghe
Tad Donaghe

Reputation: 6588

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.

Upvotes: 3

Related Questions