views:

1468

answers:

4

Hello everyone,

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

+2  A: 

I haven't needed to answer this before, but it seems like you could find the answer yourself by creating a simple little test project and trying it both ways. Pick the method that gives you the results you need. (Your post doesn't really indicate the goal, so it's hard to answer the question anyway.)

John Fisher
My goal is whether it is correct to declare OrderStatus as Datacontract. I have tested seems no differences between declaring or not-declaring, please feel free to correct me if I am wrong.
George2
That sounds like an answer then. If both situations meet your goal, then you don't need to add the extra code.
John Fisher
+3  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.

Pwninstein
Cool! About "Enums are automatically Contract'ified if they're used by another class that is part of a contract." -- I have tested from client side proxy code, it is true. But I have not found any formal WCF documents which mention this, and this is why I come here to ask. Do you have any good documents to share?
George2
+4  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.

Marc Gravell
So, no need to add DataContract to enum if I want to expose all status of the enum type through WCF?
George2
Correct. I've never once had to add it ;-p
Marc Gravell
+2  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.

Pratik