Do you use enum types in your WCF web services?

前端 未结 9 1879
臣服心动
臣服心动 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:19

    Real world story (values changed for anonymity) . Use to have an implicit enum in the application

    public enum { orange, banana, mango }
    

    Some refactoring around order and new values, and we decide to make it explicit:

    public enum { orange=1, banana=2, grape=3, mango=4 }
    

    looks inoffensive ...
    Next thing, a Website explodes. Scratch head, check service, add debug messages, all seems fine.
    Down the rabbit hole, one day later, the reason is a base wcf service that was using the enum as a return type.

    Apparenly , Wcf does not like enums with no default value.

    Fix:

    public enum { wcfBugBane=0, orange=1, banana=2, grape=3, mango=4 }
    

    So ... it might bite you.

    0 讨论(0)
  • 2020-11-27 06:28

    Enumerations are fully supported in WSDL and XSD through the xsd:enumeration schema element. It provides support for both single values and flags-style enumerations, where multiple values in a flags enumeration are separated by spaces.

    So you should have no problem using enumerations with any standards compliant platforms.

    0 讨论(0)
  • 2020-11-27 06:28

    Of course, it all depends on where you are going to use this WCF service.

    If it's a single application that will use it, then changing the contract won't have any effects.

    If it's multiple internal applications, changing the contract might require some changes on the other applications.

    And finally, if the WCF service is public, you might have to provide 2 versions of the service with differents version so that the people consuming them have the time to transfer their version of the client to the new service.

    It all depends on your needs honestly.

    0 讨论(0)
提交回复
热议问题