问题描述
我听说有人声称枚举是恶意的,不应该在Web服务中使用,因为服务器和客户端之间可能发生不匹配,如果分配了一些值,或者枚举被标记为属性。他们还表示,公开枚举的网络服务难以维护,但不能真正给我可行的论据。所以从你的经验来看,在WCF Web服务中使用枚举的优点和缺点是什么?
I've heard some people saying that enums are evil and shouldn't be used in web services because of the mismatches that could occur between the server and the client if some values are assigned, or if the enum is marked with the Flags attribute. They also said that web services exposing enums are harder to maintain but couldn't really give me viable arguments. So from your experience what are the pros and cons of using enums in a WCF web service?
推荐答案
人们建议避免的原因网络服务中的枚举是因为它们创建了微妙的向后兼容问题。
The reason people recommend to avoid enums in webservices is because they create subtle backwards compatible problems.
同样适用于常规枚举,但在Web服务中,问题更加明确,在.NET生成代理(见下文)
The same applies to regular enums but in web services the problem is even more clear specially in .NET-generated proxies (see below).
- 如果枚举输入只有您没有问题。
- 如果枚举可以是一个out参数,然后如果添加一个新元素并返回它,旧客户端可能会有问题:
- 如果客户端正在使用。 NET生成的代理它将在调用者处理它之前中断(在反序列化中)
- 即使生成的代理代码支持更改(例如,如果将枚举映射到字符串)客户端中的用户代码可能无法正确处理新的意外值(可能很容易成为一个nev执行路径)
通过将参数定义为字符串,您的API的值可能会在将来改变。即使你认为这个价值永远不会改变是一个很好的做法。
By defining the parameter as a string you signal the user of your API that the value may change in the future. Even if you think that the value will never change is a good practice to be ready.
有一个由Dare Obasanjo就此主题。
There is a good post by Dare Obasanjo on this topic.
这篇关于您在WCF Web服务中使用枚举类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!