C# – Non-defined enumeration values in WCF service

cwcf

We have a WCF service that exposes a "Customer" type, i.e.:

[DataContract(Name = "CustomerData", Namespace = "http://www.testing.com")]
public partial class Customer
{   
    [DataMember]
    public CustomerLevel Level
    {
        get;
        set;
    }   
}

You can see the above type has a property that is an enumeration type. The definition for this enum is:

[IgnoreCoverage]
[DataContract(Namespace = "http://www.testing.com"")]
public enum CustomerLevel : int
{

    [EnumMember(Value = "Platinum")]
    Platinum = 1,

    [EnumMember(Value = "Gold")]
    Gold = 2,

    [EnumMember(Value = "Silver")]
    Silver = 3,

    [EnumMember(Value = "Bronze")]
    Bronze = 4,
}

The service works fine as long as the server sends a valid enumeration for each customer that it returns. However, if the service returns a CustomerLevel that is not defined in the enumeration the service call times out.

An example of a bad CustomerLevel value might be:

customer.Level = (CustomerLevel)0;

The service also times out if the client attempts to send a non-defined value.

Is there any way to allow the non-defined value to flow through to both the client and server and let each of them handle the bad value on their own?

Best Answer

I don't think you're going to get bogus enums to work. What would they deserialize into? If you mean for the client to send integers, then change the type to int, and convert it into the enum on your own (with your own error handling).

As to whether the client should time out, please tell us what sort of client you're using. Also, I recommend you look at the network traffic and see how the service responded, if at all. Also look in the Windows event logs to see if the service complained at all. Finally, you may want to turn on WCF tracing to see how the service is reacting to this.

Related Topic