Wcf – Expose object from class library using WCF

datacontractwcf

I'm using a class library that exposes a few objects. These objects have a couple of properties that hold data my clients need. I'd like to create a WCF service that returns the objects to my clients but I cannot update the class library in order to add the DataContract and DataMember attributes. What is the easiest way of exposing these objects?

Best Answer

You can use a DataContractSurrogate.

...You can apply the DataContract attribute to the Person class, but this is not always possible. For example, the Person class can be defined in a separate assembly over which you have no control.

Given this restriction, one way to serialize the Person class is to substitute it with another class that is marked with DataContractAttribute and copy over necessary data to the new class. The objective is to make the Person class appear as a DataContract to the DataContractSerializer. Note that this is one way to serialize non-data contract classes. ...