使用WCF从类库中公开对象

时间:2023-01-15 15:41:12

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?

我正在使用一个暴露一些对象的类库。这些对象具有几个属性,可以保存客户端所需的数据。我想创建一个WCF服务,将对象返回给我的客户端,但我无法更新类库以添加DataContract和DataMember属性。暴露这些物体最简单的方法是什么?

2 个解决方案

#1


You can use a DataContractSurrogate.

您可以使用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.

...您可以将DataContract属性应用于Person类,但这并不总是可行的。例如,Person类可以在您无法控制的单独程序集中定义。

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

鉴于此限制,序列化Person类的一种方法是将其替换为使用DataContractAttribute标记的另一个类,并将必要的数据复制到新类。目标是使Person类显示为DataContractSerializer的DataContract。请注意,这是序列化非数据协定类的一种方法。 ...

#2


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.

如果无法在对象上设置[DataContract]和[DataMember]属性,则必须找到使用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.

您可以通过在服务合同或单个OperationContract上指定[XmlSerializerFormat]属性来定义服务或操作以使用XmlSerializer。

Does that help maybe?

这有帮助吗?

Marc

#1


You can use a DataContractSurrogate.

您可以使用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.

...您可以将DataContract属性应用于Person类,但这并不总是可行的。例如,Person类可以在您无法控制的单独程序集中定义。

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

鉴于此限制,序列化Person类的一种方法是将其替换为使用DataContractAttribute标记的另一个类,并将必要的数据复制到新类。目标是使Person类显示为DataContractSerializer的DataContract。请注意,这是序列化非数据协定类的一种方法。 ...

#2


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.

如果无法在对象上设置[DataContract]和[DataMember]属性,则必须找到使用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.

您可以通过在服务合同或单个OperationContract上指定[XmlSerializerFormat]属性来定义服务或操作以使用XmlSerializer。

Does that help maybe?

这有帮助吗?

Marc