Obviously WCF requires that there is an enum value equal to 0 for serialization. If you don't state any values within your enumeration, an automatic int value mapping will be done for you starting by 0, so everything's fine.
Active2 years, 3 months ago
user24770217.5k10 gold badges87 silver badges132 bronze badges
Jonathan AllenJonathan Allen40.7k62 gold badges210 silver badges400 bronze badges
4 Answers
Enums are serialized by default. Like primites and Collection classes you do not need to mark them with [DataContract]. However, that does not mean that WCF does not let you customize the serialization behavior, so in the spirit of interoperability you can change how the enum will be serialized. As part of that customizability if you mark it with DataContract but do not mark EnumMembers you are changing the default serialization scheme. See more on Enum serialization here Enum Serialization
EDIT: Got to thinking a bit more about this and now I started wondering about the underlying cause... turns out it is the WSDL's fault.
By default if you don't put [DataContract] then WCF by default serializes the enum as if it had the [DataContract] and [EnumMembers] attribute. So if you take the following example
Wcf Json Serialization
it will generate the following WSDL
So now if you take out the [EnumMember] attributes like so
your WSDL will look like this:
So the second one looks just like the first one except without the enumeration elements. Now what is the difference between the second one and just a WSDL describing a simple string? None. That is why the WCF Proxy gen gives you a string instead of the Enum.
John KJohn K
Simple - this is the way it's meant to work.
XML Schema has no concept of an enum, in the sense of a name/value pair. The closest it has is the ability to indicate that a particular simple type may have one of several values - string values in this case.
Note that ASMX web services and the XML Serializer do exactly the same thing.
OK, Jonathan and I are both right and both wrong.
When serializing an enum, WCF adds .NET-specific information to the XML Schema. This permits another .NET implementation to treat the enum as an enum, complete with preservation of the enum values.
However, no other platform is going to understand this information. As a result, an enum will simply be treated as a string which can only take on one of several values.
John SaundersJohn Saunders149k23 gold badges211 silver badges372 bronze badges
If you include the
DataContract
attribute, then you need to tag at least one value with the EnumMember
attribute. Otherwise it can't see any of the values and turns the whole field into a string.Wcf Enum Serialization Example
If you do not include the
DataContract
attribute, then you don't need the EnumMember
attribute either.EDIT: Example of correct code
Jonathan AllenJonathan Allen40.7k62 gold badges210 silver badges400 bronze badges
Because there is a defined Name for that specific value.
When there is not a defined name for a given value, then the serializer will write out an integer and the deserializer will read it just fine:
It would be nice if the serializer allowed an option to write a particular enum field as an integer because this would allow the list of valid enum names to increase without having to recompile the deserializer code. Currently the deserializer will throw an exception (and reject the whole contract) due to the unrecognized enum name.
It may be possible to work around this issue by using the [EnumMember(Value = '123')] and specify the equivalent integers for each of the enum names known at compile time. From MSDN Enumeration Types in Data Contracts
A 2nd work around is to specify the contract enum as a property that uses an integer backing store and serialize the backing store. SO Link
Community♦
crokusekcrokusek3,4701 gold badge30 silver badges49 bronze badges