WCF Rest 服务中的协方差实现
协方差概念可以在 WCF Rest 服务中实现吗?
即,我有类 A 和 B 继承自 It。
WCF 操作合约具有输入参数 A。我应该也能够将 B 传递给此操作。
我有一个 JSON 客户端,可以访问我的 EXF 休息服务。
我是否可以实现协方差概念。我应该如何在服务器和客户端中执行此操作。请帮助。
Can covariance concept be implemented in WCF rest service,
i.e, i have class A and B inherits from It.
WCF Operation contract has input parameter A. I Should be able to pass B also to this peration.
I have a JSON client which accesses my EXF rest service.
Is it possible i imeplement covariance concept . How should i do this in server and client. Pls Help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然!要实现此功能,唯一需要做的就是将 B 类添加到服务应该了解的类型列表中,使用 ServiceKnownType 属性。
下面是我整理的一个快速示例来演示这一点,假设这是您的服务契约:
以及实现:
因为您已使用
ServiceKnownType
属性告诉 WCF 关于类型Employee
的信息,当遇到它时(在输入参数和响应中),它将能够序列化/反序列化它,无论是否使用 JSON。这是一个简单的客户端:
在 WCF 服务之间传递子类型是很常见的,但您唯一不能做的就是在契约中指定一个接口作为响应。
希望这有帮助。
Of course! The only thing that's required for this to work is for you to add class B to the list of types that the service should know about using the ServiceKnownType attribute.
Here's a quick example I put together to demonstrate this, imagine this is your service contract:
And the implementation:
Because you've told WCF about the type
Employee
using theServiceKnownType
attribute, when it's encountered (both in the input parameters and in response) it'll be able to serialize/deserialize it, be it using JSON or not.Here's a simple client:
It's very common to pass subtypes to and from a WCF service, the only thing you won't be able to do though is to specify an interface as the response in your contract.
Hope this helps.