角色基础数据合约

发布于 2024-11-06 08:13:31 字数 291 浏览 1 评论 0原文

只是举个例子。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只怪假的太真实 2024-11-13 08:13:31

WCF 无法实现动态合约。您可以做的是拥有一个基本数据协定类,然后从中继承:

[DataContract]
public class MyOperationResultBase { ... }


[DataContract]
public class SpecificSchema01 : MyOperationResultBase { ... }

然后在您的服务中您可以执行以下操作

[OperationContract]
public MyOperationResultBase DoMyOperation(MyOperationRequest request) { ... }

然后根据需要返回适用的派生类的实例。

显然,这也意味着您的客户端必须检查返回结果的类型,并且您必须在基类中指定派生类的类型(或创建类型解析器,但如果该解决方案被证明有效,您可以稍后执行此操作)适合您):

[KnownType(typeof(SpecificSchema01))]
[KnownType(typeof(SpecificSchema02))]
[KnownType(typeof(SpecificSchema03))]
[DataContract]
public class MyOperationResultBase { ... }

这是您正在寻找的吗?

Dynamic contracts are not possible with WCF. What you can do is have a base data contract class, and then inherit from that:

[DataContract]
public class MyOperationResultBase { ... }


[DataContract]
public class SpecificSchema01 : MyOperationResultBase { ... }

and then in your service you can do

[OperationContract]
public MyOperationResultBase DoMyOperation(MyOperationRequest request) { ... }

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) :

[KnownType(typeof(SpecificSchema01))]
[KnownType(typeof(SpecificSchema02))]
[KnownType(typeof(SpecificSchema03))]
[DataContract]
public class MyOperationResultBase { ... }

Is this what you are looking for?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文