Do you use enum types in your WCF web services?

前端 未结 9 1998
臣服心动
臣服心动 2020-11-27 06:06

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 som

9条回答
  •  时光取名叫无心
    2020-11-27 06:15

    The reason people recommend to avoid enums in webservices is because they create subtle backwards compatible problems.

    The same applies to regular enums but in web services the problem is even more clear specially in .NET-generated proxies (see below).

    • If the enumerate is input only you have no issues.
    • If the enumerate can be an out parameter then if you add a new element and you return it, old clients could have problems:
      • If the client is using a .NET-generated proxy it will break before the caller can handle it (in the deserialization)
      • Even if the generated code for the proxy supported the change (for example if it maps the enumerate to a string) the user code in the client may not process properly the new unexpected value (it could easily be a never executed path)

    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.

    There is a good post by Dare Obasanjo on this topic.

提交回复
热议问题