Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a enum in a datacontract WCF

Tags:

I want to have an enum in a datacontract of a WCF webservice.

I am trying using

[DataContract] class myclass {     [DataMember]     public int id {get; set;}      [DataContract]     public enum myenum     {          [EnumMember]         a,         [EnumMember]         b     } } 

But I am not able to access enum at the wcf client. Please let me know what I am doing wrong.

like image 597
Vaibhav Jain Avatar asked Dec 23 '10 09:12

Vaibhav Jain


People also ask

Is Datacontract mandatory in WCF?

No, the DataContractAttribute is not required - WCF will infer serialization rules.

Why Datacontract is used in WCF?

A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts.

What is Datacontract and Datamember in WCF?

A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.

What is Enummember C#?

The EnumMemberAttribute enables fine control of the names of the enumerations as they are serialized. To use EnumMemberAttribute, create an enumeration and apply the DataContractAttribute attribute to the enumeration.


2 Answers

To be included in the mex/wsdl, the enum needs to be used somewhere in the graph, for example there should be a property somewhere of type myenum. Types not actively used in the graph are omitted.

Note also that you don't actually need to annotate enums at all - they will work just fine. You only need attributes on enums if you want to customize how they act on the wire.

like image 143
Marc Gravell Avatar answered Oct 02 '22 17:10

Marc Gravell


As Marc said - use it. The datatype itself is not an issue until it is required iirc.

Part from that its not required to have Datacontract and EnumMember on it (special rules apply). See http://msdn.microsoft.com/en-us/library/aa347875.aspx for more information.

like image 35
Tedd Hansen Avatar answered Oct 02 '22 16:10

Tedd Hansen