角色基础数据合约
只是举个例子。我有一个 WCF 服务,其方法返回数据契约。数据契约有多个数据成员,服务被多个客户端使用。数据合同中很少有成员拥有敏感信息,这些信息不应传递给所有客户。控制从 wcf 到客户端的数据流的最佳方法是什么?如果此类数据成员显示默认值,那对我来说没问题。我想避免每种类型的客户端的代码,并想使用一些配置方法。就像在配置文件中一样,我可以将所有要序列化的属性编写为逗号分隔的字符串。对于那些未序列化的,当客户端尝试访问该属性时,我可以将“未授权”异常传递给客户端吗?我要求避免使用代码的原因是,每个数据成员本身都可以计入订阅成本。可能会有很多合同,并且还在扩大。
Just giving an example. I have a WCF service with a method that returns a datacontract. Data contract has several data members, and service is being used by several clients. few members in data contract has sensitive info which should not go to all the clients. What is the best way to control the data flow from wcf to client. If such data members show default values, thats fine with me. I want to avoid code of each type of client and want to use some configuration approach. Like, in a config file, I can write all the property to be serialized as comma separated string. For those not serialized, can I pass "Not authorized" exception to the client when client try to access the property. Why I am asking to avoid code is, each data member could be itself counted for subscription cost. The there could be lots of contracts, and expanding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WCF 无法实现动态合约。您可以做的是拥有一个基本数据协定类,然后从中继承:
然后在您的服务中您可以执行以下操作
然后根据需要返回适用的派生类的实例。
显然,这也意味着您的客户端必须检查返回结果的类型,并且您必须在基类中指定派生类的类型(或创建类型解析器,但如果该解决方案被证明有效,您可以稍后执行此操作)适合您):
这是您正在寻找的吗?
Dynamic contracts are not possible with WCF. What you can do is have a base data contract class, and then inherit from that:
and then in your service you can do
Then you return an instance of the applicable derived class as needed.
Obviously this also means that your client must check the type of the returned result, and you must specify the types of the derived classes in your base class (or create a type resolver, but you can do that later if this solution is proven to work for you) :
Is this what you are looking for?