WCF 服务可以传输类型(客户端不知道这种类型)信息吗? [英] Can WCF service transmit type (client doesn't know this type) information?

查看:26
本文介绍了WCF 服务可以传输类型(客户端不知道这种类型)信息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的插件框架.WCF 客户端需要创建一个ISubject"的实例,然后发送回服务端.用户可以扩展ISubject".客户端在运行时唯一知道的是ISubject"子类的 ID.

I'm working on a simple plug-in framework. WCF client need to create an instance of 'ISubject' and then send back to service side. The 'ISubject' can be extended by the user. The only thing client knows at runtime is ID of a subclass of 'ISubject'.

首先,客户端需要获取ISubject"的特定子类的类型信息.其次,客户端使用反射枚举所有成员来创建自定义属性编辑器,以便为每个成员分配适当的值.最后,客户端创建该子类的实例并将其发送回服务.

Firstly, client need to get type information of a specific subclass of 'ISubject'. Secondly, client using reflection to enumerate all members to create a custom property editor so that each member can be asigned with proper value. Lastly, client create an instance of that subclass and send back to service.

问题是客户端如何通过WCF通信获取类型信息?

The problem is how does client get the type information through WCF communication?

我不希望客户端加载存在子类(ISubject")的程序集.

I don't want client to load that assembly where the subclass (of 'ISubject') exists.

谢谢

推荐答案

首先,您需要注意,在您描述的场景中,WCF 不会以任何神奇的方式向您的客户端提供任何类型信息.如果你要这样做,你必须自己提供一个机制.

First, you need to be aware that there is no magic way that WCF will provide any type information to your client in the scenario you have descibed. If you are going to do it, you will have to provide a mechanism yourself.

接下来,了解 WCF 并没有真正将对象从服务器传递到客户端,反之亦然.它传递的只是 XML 信息集.通常,传递的 XML 信息集包括存在于发送方的某个对象的序列化表示;在这种情况下,如果客户端知道该类型(即可以从其程序集中加载类型的元数据),它可以反序列化 XML 以在客户端实例化相同的对象.如果客户端没有类型元数据,则不能:这是 WCF 的正常情况,除非数据协定类型位于服务器和客户端实现共享的程序集中(通常不是一个好主意).

Next, understand that WCF does not really pass objects from server to client or vice versa. All it passes are XML infosets. Often, the XML infoset passed includes a serialized representation of some object which existed on the sender's side; in this case, if the client knows about that type (i.e. can load the type's metadata from its assembly), it can deserialize the XML to instantiate an identical object on the client side. If the client doesn't have the type metadata, it can't: this is the normal case with WCF unless data contract types are in assemblies shared by both server and client implementations (generally not a good idea).

WCF 的正常使用方式(例如,如果客户端是使用 Visual Studio 中的服务引用"实现的),会发生的是服务发布描述其操作的 WSDL 元数据以及操作参数的 XML 架构并返回值,并从中生成一组类型以用于客户端实现.它们与服务实现使用的数据协定类型不同,但它们是等效的",因为它们可以序列化为通过网络传递的相同 XML 数据.通常,这种类型生成是在 Visual Studio 的设计时完成的.

The way WCF is normally used (for example if the client is implemented using a "Service Reference" in Visual Studio), what happens is that the service publishes WSDL metadata describing its operations and the XML schemas for the operation parameters and return values, and from these a set of types is generated for use in the client implementation. These are NOT the same .NET types as the data contract types used by the service implementation, but they are "equivalent" in the sense that they can be serialized to the same XML data passed over the network. Normally this type generation is done at design time in Visual Studio.

为了做你想做的事情,本质上是在运行时做这种类型的生成,你需要一些机制,通过这种机制客户端可以充分了解表示各种类型对象的 XML 的结构实现 ISubject 以便它可以理解从服务接收到的 XML 并生成服务期望返回的适当 XML(直接使用 XML,或者以某种方式反序列化/序列化它).如果您真的,真的想这样做,可能的方法可能是:

In order to do what you are trying to do, which is essentially to do this type generation at runtime, you will need some mechanism by which the client can get sufficient knowledge of the structure of the XML representing the various types of object implementing ISubject so that it can understand the XML received from the service and generate the appropriate XML the service is expecting back (either working with the XML directly, or deserializing/serializing it in some fashion). If you really, really want to do this, possible ways might be:

  • 一些带外机制,通过这种机制,客户端预先配置了与它可能看到的 ISubject 的每个子类相对应的相关类型信息.盲人的回答中提供的链接是一种方法.

  • some out-of-band mechanism whereby the client is preconfigured with the relevant type information corresponding to each subclass of ISubject that it might see. The link provided in blindmeis's answer is one way to do that.

提供单独的服务操作,客户端可以通过该操作将子类的 ID 转换为子类的类型元数据(可能作为 XSD 模式,客户端可以从中生成合适的可序列化 .NET 类型来往返XML).

provide a separate service operation by which the client can translate the ID of the subclass to type metadata for the subclass (perhaps as an XSD schema from which the client could generate a suitable serializable .NET type to round trip the XML).

原则上,服务在包含序列化对象的响应头中以某种格式传递类型元数据也是可行的.客户端需要以适当的方式阅读、解释和处理类型信息.

it would also be feasible in principle for the service to pass type metadata in some format within the headers of the response containing the serialized object. The client would need to read, interpret and act on the type infomation in an appropriate fashion.

无论哪种方式,都需要付出很多努力,而且不是使用 WCF 的标准方式.你将不得不决定它是否值得.

Whichever way, it would be a lot of effort and is not the standard way of using WCF. You will have to decide if it's worth it.

这篇关于WCF 服务可以传输类型(客户端不知道这种类型)信息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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