views:

801

answers:

2

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?

+3  A: 

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

Brian
Thanks - MSDN suggests using Data Contract Surrogates but I wasn't sure if there was an easier answer.http://msdn.microsoft.com/en-us/library/ms733064.aspx
Kyle Russell
A: 

If you can't set the [DataContract] and [DataMember] attributes on your object, you'll have to find a way to expose them using the XmlSerializer.

You can define a service or an operation to use the XmlSerializer by specifying the [XmlSerializerFormat] attribute on either your Service contract, or an individual OperationContract.

Does that help maybe?

Marc

marc_s
I think Brian's answer is more close to the point. I have no control over the class library objects - his answer is closer to what MSDN suggests.
Kyle Russell