我有一个看起来像这样的WCF服务接口:
[ServiceContract(Namespace = "http://somenamespace", SessionMode = SessionMode.NotAllowed)]
public interface IRuntimeService
{
[OperationContract]
ISupporterProperties CreateOrModifySupporter(Guid id);
}
和实现(在客户端和服务器上)看起来像这样(它托管并通过编程方式连接到):
public IOwnerProperties CreateOrModifyOwner(Guid id)
{
//get an owner ...
//the owner is of type Owner which implements IOwnerProperties.
return theOwner;
}
但是,问题在这里,WCF将尝试序列化或应为Owner
,因为这是返回的实际类型,但是我希望它将其发送为OwnerDataContract
,它也恰好实现了IOwnerProperties
。<<<<<<<<<<<<<<<<<<<<
换句话说,我想返回 Owner
,但使其序列化/应为OwnerDataContract
。
我知道我可以为客户端的接口创建包装类。但是,我想拥有尽可能多的共享代码。
这对于自动应用程序来说是完美的工作。如果Owner
和OwnerDataContract
具有相同的属性,并且字段设置与
static void Main()
{
//Do this once at program startup
Mapper.CreateMap<Owner, OwnerDataContract>();
//Run the rest of your program.
}
如果您需要由于扁平化或重命名而需要重新构造一些属性,则将有更多的设置工作,请参阅Wiki。有关更多信息。
要使用映射,就像
一样简单public IOwnerProperties CreateOrModifyOwner(Guid id)
{
//get an owner ...
//Mapper.Map retuns a object of type OwnerDataContract
return Mapper.Map<OwnerDataContract>(theOwner);
}