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.
No, the DataContractAttribute is not required - WCF will infer serialization rules.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With