Welcome to EMC Consulting Blogs Sign in | Join | Help

Merrick Chaffer's Blog

Passing Enum values into WCF Service operations

24/01/2012 - Have added an update at the bottom of this article today, as it turns out that for Simple Enumerations this is not required by the DataContractSerializer.


In order to pass Enum values around to your WCF service operations, then you must put [ServiceKnownType(typeof(Enum))] attribute on the service interface contract, and also add the [EnumMember] attribute to every enum value within your Enumeration data contract.

Service contract looks like this...

namespace MyNameSpace.ServiceContracts


    [ServiceContract(Namespace = "http://MyNameSpace.ServiceContracts/2007/04", Name = "IReferenceData", SessionMode = SessionMode.Allowed)]


    public interface IReferenceData


        [OperationContract(IsTerminating = false, IsInitiating = true, IsOneWay = false, AsyncPattern = false, Action = "LookupReferenceData")]


        MyNameSpace.DataContracts.ReferenceValueCollectionData LookupReferenceData(MyNameSpace.DataContracts.ReferenceTypeData request);



Enum looks like this...

namespace MyNameSpace.DataContracts


    /// <summary>

    /// Data Contract Class - ReferenceTypeData

    /// </summary>

    [DataContract(Namespace = "http://MyNameSpace.DataContracts/2007/04", Name = "ReferenceTypeData")]

    public enum ReferenceTypeData



        Unknown = 0,


        Title = 1,


        Nationality = 2




Have just found an article that suggests for Simple enumerations this attribute decoration for EnumMember and DataContract attributes is implicit.

See the Simple Enumerations section on this msdn artcile here




Simple Enumerations

You can also serialize enumeration types to which the DataContractAttribute attribute has not been applied. Such enumeration types are treated exactly as previously described, except that every member (that does not have theNonSerializedAttribute attribute applied) is treated as if the EnumMemberAttribute attribute has been applied. 


Notes on Simple Enumerations

Applying the EnumMemberAttribute attribute to simple enumerations has no effect.

It makes no difference whether or not the SerializableAttribute attribute is applied to the enumeration.

The fact that the DataContractSerializer class honors the NonSerializedAttribute attribute applied to enumeration members is different from the behavior of the BinaryFormatter and the SoapFormatter. Both of those serializers ignore the NonSerializedAttribute attribute.


Published Tuesday, April 03, 2007 3:35 PM by merrick.chaffer


No Comments
Anonymous comments are disabled

This Blog



Powered by Community Server (Personal Edition), by Telligent Systems