Tuesday, October 28, 2014

Exception Handling in Web API 2

When working with Web API 2, unhandled exceptions thrown by controllers are returned to the client as web HTTP status code 500 (Internal Server Error).  An exception filter can be used to replace this generic error with something a little more meaningful.  Another benefit to using exception filter is that it centralizes exception handling.  This means that the most of the time you will not need a try catch statement in your controller methods. Exception filters will catch any exceptions thrown by a controller method that is not an HttpResponseException.

For debugging purposes I decided I wanted to return the entire exception message in my response. For release I have several exception filters targeted to specific exceptions that return meaningful messages to the client. 

To create an exception filter, derive from System.Web.Http.Filters.ExceptionFilterAttribute class and override the OnException method.

Exception filters can be applied as an attribute to the entire controller class, to individual methods, or registered globally in the WebApiConfig.  I registered mine with the debug compiler switch to ensure that this particular exception filter is not used when compiled for release.

One more thing to consider is the status code your sending back with the exception. Sending back the correct exception is important to help the client determine what has gone wrong.  Brockallen has a nice guide for which HTTP status code to use on his blog

This takes care of the majority of error that are a concern when building out a Web API solution, however there are still exceptions that will not be caught by exception filters. That is where Web API Global Error Handling comes into play.

Saturday, October 18, 2014

C# Conventions & Best Practices - devdoc2013

Several months ago I ran across devdoc2013 over on David Anderson's blog.  The sub title of this paper sums it up "Programming conventions, and general developer best practices for software developers writing C# with .NET."  Good stuff!

Wednesday, October 15, 2014

Sorting ListCollectionView as Object Values Change

Recently, a new requirement popped up.  The requirement was to sort a list of objects in a data-grid, bound to a ListCollectionView as object values in the collection were changed.  In our case, the object type we wanted to sort on was a double.

At first I tried to implement this using a SortDescription, but I quickly realized this had one drawback.  The SortDirection seems to set an alphanumeric sort on the data column.  Values (1, 2, 10, 200, 100) would be sorted as (1, 10, 100, 2, 200) which was precisely not what I wanted.   To get a pure numeric sort it seemed I'd have to write my own logic. 

Thankfully the list collection view provides another property CustomSort, which takes an IComparer<T>.  The MSDN docs mention that it's preferred to derive from Comparer<T> instead of the interface.  With that in mind I was able to create the following class. 

This custom sorter was then wired into my ListCollectionView like so.
This provided the desired functionality of sorting each row into the correct position as the values were edited.
Row in edit mode is not sorted

Edit completed and row is sorted into place

While this worked great for what we were doing, I can't speak to how this would perform with extremely large sets of data.  

Sunday, October 5, 2014

XAML - Vertical & Horizontal GridSplitters

Often it's nice to allow the user to re-size segments of the screen.  WPF makes this possible with the GridSplitter class.   The MSDN documents are pretty clear on its usage with examples but here is another example I created in XamlPadX.

Horizontal & Vertical GridSplitter 

Beware, if you have a parent container that is scroll-able you will have trouble getting the GridSplitter to work correctly.   I made this mistake while working with PRISM.  Initially in the shell I wrapped my main content region with a ScrollViewer.  It seemed like a good idea at the time and I promptly forgot I'd done so.  While working in a module that loaded into the main content area, I noticed that my horizontal GridSplitter between two ListBox controls was not working correctly.  When adding items to the top list box, the grid splitter would be pushed down the screen.  Using WPF Inspector, I was able to find the issue with the ScrollViewer in the shell.