Using an enum type with WCF services

I recently started to work with Windows Communication Foundation and one of the first problems I did run into was how to use enum type in WCF.

This is a small example of a data contract and a service contract/interface that makes use of the enum type.

using System;
using System.ServiceModel;
using System.Runtime.Serialization;
 
namespace Test.DataContracts
{
    [DataContract(Namespace = "Testing", Name = "JobResult")]
    public enum JobResult
    {
        [EnumMember]
        Passed = 0,
        [EnumMember]
        Failed = 1,
        [EnumMember]
        Running = 2,
        [EnumMember]
        TestFailure = 3,
        [EnumMember]
        Queued = 4,
        [EnumMember]
        JobNotFound = 5
    }
}

using System;
using System.ServiceModel;
using Test.DataContracts;
 
namespace Test.ServiceContracts
{
    [ServiceContract(Namespace = "Testing")]
    [ServiceKnownType(typeof(JobResult)]
    public interface IJob
    {
        [OperationContract]
        JobResult GetJobResult(string job);
        ...
    }
}
3.67 avg. rating (76% score) - 3 votes

About Peter Wibeck

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

*