WCF:双方强制执行平等的数据契约
我想知道是否可以让 WCF 确保连接两侧的 DataContracts 完全相同(如果不同,则在尝试连接时抛出异常)。
例如,想象一下这个服务:
[DataContract]
enum State
{
[EnumMember]
Red,
[EnumMember]
Yellow,
[EnumMember]
Green
}
[ServiceContract]
interface MyService
{
[OperationContract]
void SetState(State newState);
}
现在想象一下该服务已更新并且现在支持新的状态“Orange”。客户端仍然拥有如上所示的 DataContract。
现在我希望客户端对服务的每次调用都失败,因为客户端和服务没有使用相同的 DataContract。这可能吗?
预先感谢您的任何帮助!
I'm wondering if it is possible to have WCF make sure that the DataContracts on both sides of a connection are exactly the same (and throw an exception when trying to connect if they are not).
For example, imagine this service:
[DataContract]
enum State
{
[EnumMember]
Red,
[EnumMember]
Yellow,
[EnumMember]
Green
}
[ServiceContract]
interface MyService
{
[OperationContract]
void SetState(State newState);
}
Now imagine the service is updated and now supports a new State, "Orange". The client still has the DataContract as shown above.
Now I want every call from the client to the service to fail because client and service are not using the same DataContract. Is this possible?
Thanks in advance for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您实际上无法做那么多 - 但您可以使用 XML 命名空间对数据协定进行版本控制 - 如下所示:
您的客户端现在将使用此数据协定 - 与 XML 命名空间。
如果您下个月在服务器上更改数据协定,则可以将 XML 命名空间更改为:
如果您停用所有使用“/2009/11”数据协定的服务端点,并且只有使用新数据协定的新端点,则客户端将无法再成功调用您的服务方法(因为两个 DataContract 的 XML 命名空间不匹配)。
也许这会是一条路?
马克
Well, you can't really do all that much about - but you could version your data contract with XML namespaces - something like this:
Your client will now use this data contract - with the XML namespace.
If you change your data contract on the server next month, you could change the XML namespace to:
If you retire all service endpoints that used the "/2009/11" data contract and only have new endpoints with the new data contract, the clients won't be able to successfully call your service methods anymore (since the XML namespaces of the two DataContracts don't match).
Maybe that would be a way to go?
Marc