WCF版本控制枚举 [英] WCF Versioning Enums

查看:202
本文介绍了WCF版本控制枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始编写一些需要向前兼容的WCF数据合同可版本。我一直在阅读MSDN文章这里,并想知道是否有人对第14号有关枚举的说明。它如下所示:


14。您不应添加或删除枚举成员版本之间。您也不应重命名枚举成员,除非您使用EnumMemberAttribute属性上的Name属性将其名称保留在数据合约模型中。




阅读这篇文章,我认为,一旦枚举被发布(并被客户使用),你无法修改它(主要是添加/删除)而不会破坏兼容性? (即这将是一个突破性的变化)



有人可以确认吗?

解决方案>

我可以确认您可以添加到发布的枚举,而不会破坏兼容性,只要在与服务通话时不要使用新值。但是请注意,如果您确实尝试向正在使用新枚举值的服务发送一个类,则会得到一个System.ServiceModel.CommunicationException。

 尝试序列化参数myType时出错。 InnerException消息是'枚举值'x'对'myType'类型无效,不能被序列化。如果类型具有DataContractAttribute属性,请确保存在必需的枚举值,并使用EnumMemberAttribute属性标记。有关详细信息,请参阅InnerException。 


I'm beginning writing some WCF data contracts that need to be forward-compatible & versionable. I've been reading the MSDN article here, and was wondering if anyone has clarification on point #14 regarding enums. It reads as follows:

14. You should not add or remove enumeration members between versions. You should also not rename enumeration members, unless you use the Name property on the EnumMemberAttribute attribute to keep their names in the data contract model the same.

Reading this, I take it to mean that once an enum is published (and used by clients), you you cannot modify it in anyway (adding / removing mainly) without breaking compatibility? (i.e. this would be a breaking change)

Can someone confirm this?

解决方案

I can confirm that you can ADD to a published enum without breaking compatibility, as long as you don't use the new value when talking to the service. However be aware that if you actually try to send an class to the service that is using the new enum value, you will get a System.ServiceModel.CommunicationException.

There was an error while trying to serialize parameter myType. The InnerException message was 'Enum value 'x' is invalid for type 'myType' and cannot be serialized. Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute if the type has DataContractAttribute attribute.'.  Please see InnerException for more details.

这篇关于WCF版本控制枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆