tags:

views:

550

answers:

3

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?

A: 
Andrew Hare
I removed the DataContract and EnumMember attributes from the enumeration and the behavior is still the same.The service only works when I send a valid enumeration value. It times out if I send an invalid value.
Andrew -- you're right, the DataContractSerializer blows up if an invalid enumeration value exists.Why, though, does the call not cause an exception? It simply times out.
Very strange - it threw an exception for me when I did a test.
Andrew Hare
The DataContractSerializer throws an exception, as expected. But when I make a call from a client, using the same scenario, I simply get a timeout.
I see the exception thrown in the client.
Andrew Hare
+1  A: 

I don't think there is any way to expose undefined enums using a DataContract.

For propagating exceptions, you might try enabling "IncludeExceptionDetailsInFaults" - see the MSDN documentation for details.

What I usually do is implement an IErrorHandler server-side that logs exceptions and promotes exceptions to Faults - there is some good stuff on IDesign's web site.

Joe
+2  A: 

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.

John Saunders