2011-06-17

WCF "Gotcha" When Sending an Invalid Enum Value Across the Boundary

The Silverlight application I'm working on for my employer at the time of this writing involves WCF services that provide data for the app.

It was working fine until a coworker ran a SQL query that changed an int field (which maps to an Enum in our code) to an invalid value on several records in a common test database a few of us were pointing to.

All of the sudden we were getting a FaultException (I can't remember the exact error - but it was one of those unrelated ones that mask the real exception message that caused the problem to begin with.) while trying to load a graph.

Further investigation revealed that our asynchronous calls to the WCF service client were retrying the same call three times and coming back with a failure.  Stepping down deeper into the code, we saw that our resultset seemed to be coming back fine from our data layer, leaving us wondering why it was behaving so strange.

My coworker put a breakpoint where our data layer returns the IEnumerable to the service so it can serialize/return the data to our app, and changed the resultset to an empty array of the given type instead of allowing the real resultset through.

Only a single attempt was made on the service instead of three and the data was received fine by the application...  However, when we reverted to allow the regular resultset through, it again made three attempts and returned the exception.

On the next run, we decided to look at the resultset closer in the QuickWatch window.  We didn't look carefully enough because everything looked correct... so we started removing one object at a time until we could prove that the culprit was corrupt data in our resultset.

Finally we expanded two objects in our collection at the same time in the QuickWatch window and started comparing.  We noticed that one instance's enum property was retuning the name of the assigned enum and another showed an integer value.

After asking why that would happen, it dawned on me that it must be an invalid enum value and therefore couldn't resolve to a name when ToString() was called.  I assume to fail gracefully, Microsoft had their Enum.ToString() try to do name first and just fall back to the value of the Enum's base type if it couldn't resolve the name, rather than throwing an Exception.

Examining the implementation of the enum quickly confirmed the suspicion.  After probing other coworkers, we discovered the erroneous query that had been run against the database.  :)

I assume what happens is an exception occurs when the WCF tries to serialize the object to send across the boundary... but the exception is swallowed up and the Fault Exception with the less-than-helpful error message is returned to the consumer of the service.

Moral of the story: Be careful when using Enums that map to data in your database.  Make sure they always match the allowed values in the database (which is easy to do if you have look-up tables and use an ORM (like PLINQO, etc.) to generate the enums for you. 

Also, if you use "switch" statements in your code on the enum value, make sure your default case handles an invalid enum value scenario.  For instance, you've made a switch statement that contains a case block for all valid enum values.  Your default case can throw an ArgumentOutOfRangeException so you can quickly detect when something like that happens.

This will be harder to detect in scenarios like the one I described above, because the code is still "valid" at compile and runtime until the sucker is serialized.  I don't know what the best practice solution is for that scenario, but I do know I want a better FaultException passed to me if serialization breaks before being passed from the WCF service.

Guess I have my work cut out for me tomorrow.

For those like me that have a hard time visualizing what I'm talking about... here's a contrived example.

public enum ReportType
{
  Html = 1,
  Excel = 2,
  Pdf = 3
}

The query to the database changed the offending records to 0, 4 or some other number that wasn't the valid values.  (Incidentally, my coworker really meant to change another int field's value... but ended up typing the wrong column name.)   Too funny.

And now you know... go forth and make sure you aren't bit by this problem.

Silverlight and Unit Testing - Reference Gotcha When Testing View Code

I'm new to Silverlight and am still learning my way around various nuances of the platform.

I like it so far and have been learning a lot. When a recent check-in was made by a co-worker to our project at work, we had around 90 of over 800 unit tests broken with this error in each of them:
The attachable property 'TransitionEffect' was not found in type 'ExtendedVisualStateManager'.

Being new to XAML, I had no idea where to start, except to go find the .xaml files resource dictionaries to try to figure out what the problem was.  Searching the internet made it sound like I had x:Name attributes with the same values from different dependent XAML files.

I should mention I was working in a UserControl XAML file to begin with, and started searching up the dependency hierarchy to see if the naming problem was the real issue.

The XAML files were quite large, so finding all the x:Name attributes and comparing them was going to take a while.  I opted to approach this using LINQPad.  I replaced quotes with double-quotes in the XAML so I could bring it into LINQPad as a string constant and loaded it into a XDocument object; using LINQ to XML to find matches from the other file's XAML for me.  After discovering one, I fixed it and re-ran my unit tests.  I still got the same error.

What I didn't understand was that the application ran fine and the effect was working... why on earth wouldn't it work in our unit tests?  It should have dawned on me that both the application project and the unit test project required the appropriate DLLs to be referenced in order for that namespace to be available.

Sure enough, the application project had them but the unit test project did not.  As soon as I added the references, the tests passed with flying colors again.

The required references, for those that get the exact same error:
Microsoft.Expression.Effects
Microsoft.Expression.Interactions
(both part of the Silverlight v4.0 Toolkit)

Moral of the story: When you add references to your application project, make sure you add them to the corresponding unit test project if necessary as well.

Now the next question... Why was I required to have those when I wasn't rendering any UI, but merely using the View classes in the unit test?  It seemed as if the XAML was still being parsed.

This makes sense, being as your View classes are declared as partial and the other piece to this partial puzzle is the XAML file.  Remember that whenever the View class is constructed, the InitializeComponent method is called, which parses the XAML and sets up everything declared there (correct me if I'm not describing that accurately!)

That said, if you're going to unit test a view file, you have to make sure that anything required by the XAML files are also referenced.  It's easy to take care of that for the C# files because you get compile-time errors, but you don't necessarily with the declarative XAML files.  A drawback, but one that can be handled if we're aware.

Bottom line... remember that both the XAML and corresponding C# file make up your View class and both files' reference requirements must be met to ensure all namespaces/code are available for your unit tests.

I hope this will save you (and myself) a lot of time in the future if I ever encounter this problem again.