WCF和数据契约接口 [英] WCF and interfaces on data contracts

查看:253
本文介绍了WCF和数据契约接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用SvcUtil工具创建WCF代理,是否有可能包括接口以及从该数据契约继承,如:

While creating the WCF proxy using svcutil, is it possible to include the interfaces as well from which the data contracts inherit, e.g.:

public class SomeType: ISometype
{
   public string Name { get; set; }
}

public interface ISometype
{
   public string Name { get; set; }
}

当我创建一个使用这个代理,在SOMETYPE类型是在客户端创建,但尚未创建的接口并没有继承任。我试图标记接口DataContract,但允许该属性未启用。

When I create the proxy using this, the SomeType type is created at the client but the interface isn't created and there is no inheritance either. I tried marking the interface as DataContract but that attribute isnt allowed.

是否有可能做什么,我想干什么?

Is it possible to do what I am trying to do?

推荐答案

WCF使用序列化的消息,而所有这些消息需要能够使用的DataContractSerializer或一个XmlSerializer进行序列化。和去客户端之间的那些消息和服务器需要前pressible在XML模式

WCF uses serialized messaging, and all those messages need to be able to be serialized using a DataContractSerializer or an XmlSerializer. And those messages going between the client and the server needs to be expressible in XML schema.

现在,XML模式不知道事情的接口 - 这是所有关于具体的,实际的类型。对于一个普通场景,我们的客户可以从.NET什么PHP和Ruby到(其他),你需要确保你想要的,可以重新$一种客户端和服务器之间发送EX preSS一切p $ psented XML模式 - 接口不能。所以真的没有办法来支持这个在通用的场景。

Now, XML schema doesn't know anything about interfaces - it's all about concrete, actual types. For a regular scenario where your clients can be anything from .NET to PHP to Ruby to (whatever), you need to make sure to express everything you want to send between client and server in a way that can be represented in XML schema - interfaces cannot. So there's really no way to support this in a general purpose scenario.

如果你控制线,如两端你写的客户端和服务器,无论是在.NET中,那么你可以这样做:

If you're controlling both ends of the wire, e.g. you write both the client and the server, and both in .NET, then you can do this:

  • 把你的DataContracts(和你的ServiceContracts和OperationContracts和FaultContracts)都变成一个独立 MyServiceContracts 组装

引用组装来自服务端code,以及客户端。在这种情况下,当你去创建客户端代理,这些你提到的类型已经是present和WCF会很乐意从组件重用这些类型。而且因为这是你引用一个.NET程序集,你可以在里面有什么是.NET支持 - 包括接口

reference that assembly from both your service-side code, as well as the client. In that case, when you go about to create the client proxy, those types you mention are already present and WCF will happily reuse those types from that assembly. And since that's a .NET assembly you're referencing, you can have anything in there that .NET supports - including interfaces.

这篇关于WCF和数据契约接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆