本文介绍了WCF DataContract-标记成员IsRequired = false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的合同如下:

是IsRequired=false表示我可以使用StrB统一通过导线传递MyObj的实例吗,还是意味着我可以在StrB不在的情况下通过导线传递MyObj的实例?

What exactly does IsRequired mean? Does IsRequired=false mean that I can pass an instance of MyObj across the wire with StrB unitialized or does it mean that I can pass an instance of MyObj across the wire with StrB absent?

如果是后者,我该如何实例化+发送不带StrB的MyObj实例?

If the latter, how do I actually instantiate + send across an instance of MyObj without StrB?

推荐答案

DataMember 的IsRequired告诉序列化引擎是否必须在基础XML中显示StrB的值.

DataMember's IsRequired tells the serialization engine whether the value of StrB must be presented in the underlying XML.

因此,通过线路您可以获得<MyObj></MyObj>,它将很好地反序列化为MyObj实例.

So over the wire you can get <MyObj></MyObj> and it will deserialize into an MyObj instance just fine.

在没有StrB的情况下,您实际上无法初始化MyObj的实例.这样做的用途是兼容性和可扩展性.例如,客户端可能没有更新的MyObj版本,也没有StrB.在这种情况下,服务器代码可以将StrB标记为未请求,并且在服务器端收到消息时不会出现序列化异常.

You can't actually initialize a instance of MyObj without StrB being present. The use for this is compatibility and extensibility. For example, maybe the client doesn't have the updated MyObj version and it doesn't have StrB present. In this case, the server code can mark StrB as not requried and there will not be a serialization exception when a message is received on the server side.

这篇关于WCF DataContract-标记成员IsRequired = false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 00:30