Cleaning up WCF clients using Interception


If you have ever used the using statement to create a WCF client proxy before, you would have no doubt encountered problems of not getting your Service Exceptions propagating to the client. When exceptions are raised from service, your client proxy goes into a Faulted state, and attempting to close/dispose the proxy object (from the using statement) will result in WCF raising another Exception.

If you’re aware of this, you would have written code somewhat similar to this, to do proper cleanup.

SomeClient client = new SomeClient();

try
{
    client.SomeMethod();
    client.Close();
}
catch (CommunicationException)
{
    client.Abort();
}
catch (TimeoutException)
{
    client.Abort();
}
catch (Exception)
{
    client.Abort();
    throw;
}

Read the rest of this entry »

Fun with Attribute Programming


Attribute Programming is a very powerful technique. You would have encountered Attributes in .NET framework, e.g. [Serializable] attribute. It’s particularly useful for cutting across concerns such as Logging, Authentication, Authorization, Caching and Exception Handling. Typically in OO programming we structure our code in layers, and often find that we tend to repeat code to perform logging and exception handling across these layers. This is where cross-cutting is used to cut across these layers and helps to encapsulate and separate our concerns (typically known as Aspect Oriented Programming). If you’re keen on AOP, have a look at the Unity Application Block.

Read the rest of this entry »