WCF EnumMember WSDL Caveot

A customer emailed me the other day, after pulling the WSDL for one of our WCF web services, and basically said “I don’t understand your enum values, what do the values mean, like item1, item2, etc…?” This confused me, so I had to do some digging.

I made a new .NET 2 solution in Visual Studio 2005 and pulled my WSDL. Now, I know that in my code, I have an enum defined like:

    [DataContract]
    public enum OrderType
    {
        [EnumMember(Value = "1")]
        PartOrder = 1,

        [EnumMember(Value = "2")]
        PriceCheck = 2,
    }

The [EnumMember] attribute indicates that the value should be serialized into the data contract. I specified the Value property also, and was always setting it to be the same as the enum’s value. The Value property’s documentation, from MSDN: “By default, the value of an enumeration is the name of the enumeration. When a value is specified, that value will appear in the XML document or stream.” In simpler terms, it specifies what value will be sent over the wire for this enum. So for example if I serialized OrderType.PriceCheck, WCF would send “2″ over the wire. Setting the Value to be the same as the enum’s value seems to make sense. It also reduces the message size, since by default if no Value was set, it would send “PriceCheck” over the wire. So I just saved myself 9 bytes per message right there! Hurray! (well, not so much…)

As it turns out, there is another implication to explicitly setting the Value property. When WCF generates the WSDL for the enum, you end up with something like:

  <xs:simpleType name="OrderType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="1">
        <xs:annotation>
          <xs:appinfo>
            <EnumerationValue>1</EnumerationValue>
          </xs:appinfo>
        </xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="2">
        <xs:annotation>
          <xs:appinfo>
            <EnumerationValue>2</EnumerationValue>
          </xs:appinfo>
        </xs:annotation>
      </xs:enumeration>
    </xs:restriction>
  </xs:simpleType>

Now when a client pulls the WSDL as a Web Reference in Visual Studio, it will attempt to generate a class for the types. As you can see from the WSDL snippet, there isn’t much for it to go by, so what it ends up creating is:

    public enum OrderType
    {
        item1 = 1,
        item2 = 2,
    }

The issue being that only the Value from the EnumMember attribute is used to identify the enum in the WSDL. So if I would have created by enum like:

    [DataContract]
    public enum OrderType
    {
        [EnumMember(Value = "Foo")]
        PartOrder = 1,

        [EnumMember(Value = "Bar")]
        PriceCheck = 2,
    }

then client side, they would end up seeing:

    public enum OrderType
    {
        Foo = 1,
        Bar = 2,
    }

So the “fix” for this is to just set a meaningful Value property, or don’t include one at all. Then it will default to the actual name, and the customer will get the same enum that you originally started with. Of course, the downside to this is that what is serialized in the message is bigger than what it needs to be, but its better than publishing a whole “item1 = Something, item2 = Something Else” cross reference guide for your customers!

If anyone knows of another way to control what is serialized vs the generated class, I’d love to know about it…

Advertisements
Tagged with: , , ,
Posted in Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

CodingWithSpike is Jeff Valore. A professional software engineer, focused on JavaScript, Web Development, C# and the Microsoft stack. Jeff is currently a Software Engineer at Virtual Hold Technologies.


I am also a Pluralsight author. Check out my courses!

%d bloggers like this: