Q:

DataContract issue in WCF

Suppose I have a method and the return type is enum, my question is should I declare the enum as DataContract or not?
Samples like, in the sample, OrderStatus is an enum data type,
OrderStatus Poll(string queryID);

Should I declare OrderStatus enum type as DataContract?
thanks in advance,
George

A:

I don't think you need to declare OrderStatus as a DataContract. In my experience, Enums are automatically Contract'ified if they're used by another class that is part of a contract.
Also, there exists an [EnumMember] attribute that lets you choose which enum values you want exposed via your contract, should you not want to expose all of them for some reason.

A:

The only time you need to add [EnumMember] etc to the enum is if:

you don't want them all exposed (see Pwninstein's answer)
you want to change the text on the wire for compatibility reasons (set Value).

MSDN has an example of the latter.

A:

If you care to publish your wsdl with proper xsd namespaces you can only do this using the DataContract attribute for the enum.
e.g.
[Datacontract(Namespace="http://company/xsd/service/2009/07/03"]
public enum Status
{
[EnumMember]
ERROR = 1,
[EnumMember]
GOOD = 2,
}

Otherwise the default namespace is used in wsdl. For an enterprise webservice you may wish to have proper namespace and version control your wsdl schemas. Also using EnumMember attribute you can opt out from publishing all your enum values as mentioned above.

