wcfweb-servicesknown-types

WCF, return list of Known Types


I have a server side service called ConstructionManager, one of its operation is GetAll() which return a list of constructions. Construction is a data contract, and there are several types which inherit from Construction (Buildings, Apartments etc..)

When I send the list of apartments all is good, all properties is on their place, but when i receive that list at client side, and see what it is in the received object at run time, in Non Public Members i saw all the properties that are specific to type that inherits from Construction, like Rooms, Floor, but in Result View it shows all properties have the value "0", and not the value with which they were sent.

On data contract Construction, at the top of class, are KnownType attributes to inherited classes.

It maybe helpful to know, I use Web Service Software Factory.

Sorry for my bad English.

First image: Server Side GetAll() method, Floors have value: "2"

Second image: Client Side Non public members of list, value of Floors="2"

Third image: Client Result View of  list, value of Floors="0"


Solution

  • If you see that properties are populated in an object in your service just before it is sent over the wire to the client (i.e. just before serialization), and then see that the received object is missing the values in those properties just after it is received by the client, it means that they were lost in the serialization process.

    There are 2 things you need to remember about serialization:

    1. You need to make sure the classes you send over the wire are marked with the [DataContract] attribute, and that all properties within that are marked with the [DataMember] attribute. If a property is not a .NET type, then the class that defines (and the properties within it) it also needs to be marked up with these attributes.

    2. Class Inheritance is lost in serialization. If you create an object of type "Building", and your WCF service method returns a type of "Construction", then the message sent to the client might not serialize correctly (eg. could it only be serializing the properties defined in the base type "Construction"?). I suggest you test this out by making your service return the inherited type rather than the base type, and see if it fixes the problem.

    I think that the 2nd point is the most likely cause of your issues. If you provide your code I can help you in more detail