views:

125

answers:

1

I tried to look this up online, but all the WSDL examples seem to not really explain if I should mark things as basetype string in the WSDL or int...

Basically, I'm trying to make my WSDL so that I can represent an Enumeration. I have a C# Enum in mind already that I want to match it up to...

public enum MyEnum {
    Item1 = 0,
    Item2 = 1,
    Item3 = 2,
    SpecialItem = 99
}

I'm not sure how my WSDL should look... I figure it's one of two, but even then I'm not 100% sure...

<wsdl:types>
    <xsd:schema targetNamespace="http://www.mysite.com/MyApp"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                >
        <xsd:simpleType name="MyEnum">
            <xsd:restriction base="xsd:int">
                <xsd:enumeration value="0" />
                <xsd:enumeration value="1" />
                <xsd:enumeration value="2" />
                <xsd:enumeration value="99" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:schema>
</wsdl:types>

OR

<wsdl:types>
    <xsd:schema targetNamespace="http://www.mysite.com/MyApp"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                >
        <xsd:simpleType name="MyEnum">
            <xsd:restriction base="xsd:string">
                <xsd:enumeration value="Item1" />
                <xsd:enumeration value="Item2" />
                <xsd:enumeration value="Item3" />
                <xsd:enumeration value="SpecialItem" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:schema>
</wsdl:types>
+3  A: 

Enumerations will end up looking like their string representations. So the correct wsdl will present the enums as:

<xs:simpleType name="MyEnum">
    <xs:restriction base="xsd:string">
      <xs:enumeration value="Item1" />
      <xs:enumeration value="Item2" />
      <xs:enumeration value="Item3" />
      <xs:enumeration value="SpecialItem" />
    </xs:restriction>
  </xs:simpleType>

The above will automatically serialize/deserialize to the MyEnum enumeration type for you. If you present the enums as xsd:int then you will end up having to convert them manually back and forth.

You can refer to the enumeration definition within your schema like so:

<xsd:complexType name="Class1">
    <xsd:sequence>
      <xsd:element minOccurs="1" maxOccurs="1" name="MyEnumProperty" type="MyEnum" />
    </xsd:sequence>
  </xsd:complexType>
code4life
@code4life: How does it know that I want SpecialItem to be 99? Or does it not care how it's actually implemented? Basically when I create a response message do I just add MyEnum to it and it serializes it in a way that it doesn't store the base int value of the enum, but rather the string name so the other end can receive it by name instead of the base value? Does that make sense?
myermian
The serializer doesn't actually care. Just make sure to set the values to the MyEnum type, not an integer. Also you need to always ensure that your server-side and client-side definitions for the MyEnum enumeration are identical, otherwise you'll get undesirable results.
code4life
So basically it serializes the name (i.e. "SpecialItem") and it doesn't care what SpecialItem is assigned... but, for my own clarity I should assign them both 99 on Client and Server? Would it mess up if I assigned it 100 on the Client and 99 on the server assuming I don't account for that in their own separate programming?
myermian
Yeah, it would mess up in a subtle way, if those numbers happen to have some special meaning. Basically the challenge is having one enumeration that is shared between two separate layers. So you need to figure out how you want to make these layers handle the enumeration in a common manner. It would probably lead to bad results if each layer synchronized the results a bit differently.
code4life