您是否在 WCF Web 服务中使用枚举类型? [英] Do you use enum types in your WCF web services?

查看:22
本文介绍了您是否在 WCF Web 服务中使用枚举类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说有人说枚举是邪恶的,不应该在 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?

推荐答案

人们建议避免在 Web 服务中使用枚举的原因是因为它们会产生微妙的向后兼容问题.

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).

  • 如果仅输入枚举,则没有问题.
  • 如果 enumerate 可以是一个 out 参数,那么如果您添加一个新元素并返回它,旧客户端可能会遇到问题:
    • 如果客户端使用 .NET 生成的代理,它会在调用者处理它之前中断(在反序列化中)
    • 即使为代理生成的代码支持更改(例如,如果它将枚举映射到字符串),客户端中的用户代码也可能无法正确处理新的意外值(它很容易成为从未执行过的路径)

    通过将参数定义为字符串,您可以向 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 服务中使用枚举类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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