将 DataMember 添加到 WCF 中的现有 DataContract
我想将 DataMember
添加到我的一个 DataContract
中。我想知道如果其中一方未更新,现有服务器和客户端在出现新的 DataMember
时将如何表现。
我记得有一种方法可以使 DataMember
可选,但我想知道它是否适用于所有场景:
- 更新的 Client =>旧服务器
- 旧客户端=>更新的服务器
- 更新的客户端 <= 旧服务器
- 旧客户端 <= 更新的服务器
I would like to add a DataMember
to one of my DataContract
s. I would like to know how existing servers and clients will behave in the presence of a new DataMember
if one of the parties isn't updated.
I recall there is a way to make the DataMember
optional, but I wonder if it would work in all scenarios:
- updated Client => old Server
- old Client => updated Server
- updated Client <= old Server
- old Client <= updated Server
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WCF 将优雅地处理它无法识别的新成员。合约的使用者(无论是在客户端还是在服务器端)根本不会“看到”该成员,因此结果是新成员永远不应该是
IsRequired=true
属性。此外,只要 DataContract 实现
IExtensibleDataObject
,WCF 就会在组件之间透明地桥接新属性。 EG,如果消息路径为:updated client =>旧服务器=>更新的服务器
,那么链末端的更新服务器仍将看到新的 DataMember。但是,“旧服务器”将看不到该新 DataMember。
如果旧服务器向更新的客户端发送消息,则新的 DataMember 将在新客户端中反序列化时设置为
default(type)
。此处有关于 DataContract 版本控制最佳实践的更多信息。
本文讨论重大变更和非重大变更之间的区别。
WCF will gracefully handle new members that it doesn't recognize. The consumer of the contract (on either the client or the server side) simply won't "see" that member, therefore a consequence is that the new member should never be an
IsRequired=true
property.Furthermore, WCF will transparently bridge the new property between components as long as the DataContract implements
IExtensibleDataObject
. EG, if the message path goes:updated client => old server => updated server
then the updated server on the end of the chain will still see the new DataMember. However, the "old server" will not see that new DataMember.
If an old server sends a message to an updated client, then the new DataMember will be set to
default(type)
upon deserialization in the new client.There's more about Best Practices for DataContract versioning here.
And this article discusses the difference between Breaking and Non-Breaking changes.