Skip to main content


Showing posts from September, 2010

Invoking System.Actions with Retries using ReliableActionInvoker

Although some would argue that Exceptions should be thrown and handled one level up. I tend to think there are plenty of cases where by after an Exception is thrown it makes total sense to Retry the operation N number of times before finally Re-throwing the Exception up the Call stack. One of these such cases is the simple act of Downloading a file from a Remote Server. Although the code is very simple, WebClient.DownloadFile  can and does fail frequently for a host of different reasons. Consider the following example which simply tries to download a remote Uri to a local file path. private int maxRetries = 5; private int retriesCounter; public void Download(string uri, string targetFilePath) { try { var webClient = new WebClient(); webClient.DownloadFile(uri, targetFilePath); retriesCounter = 0; } catch (WebException webEx) { …

OpenRasta vs WCF for RESTful Services

Not long ago I discovered OpenRasta. If you’re not familiar with it it’s described as a “resource-oriented framework for .NET enabling easy ReST-ful development of websites and services”.After reading Rails 3 in a Nutshell I started looking at .NET a bit differently and have started being envious of the simplicity of Rails. When it comes to REST the ROR guys have definitely approached it the right way and it is baked right into the Rails framework.When it comes to .NET there are a few different approaches you can take to create RESTful services.ASP.NET MVC WCF OpenRasta Other MVC Frameworks Standard HTTP Handlers In this post I’m going to cover how you can implement such a service using WCF and OpenRasta.The goal is to expose a Product as JSON and XML using a RESTful uri.

product/1 WCF ApproachFirst let’s have a look at how to do this in WCFProject Structure Just create a standard WCF Service Application ProjectDataContractThe first thing we have to do is create a DataContract whic…

Defensive Programming & Argument Validation

The other day I found myself debugging the dreaded NullReferenceException “Object Reference not set to an instance of an object” on a piece of code written by a fairly Senior Developer. If you’ve been developing on .NET for more than 5 minutes then I’m sure you have come across this in your daily work and it is incredibly frustrating to try and debug. With this post I hope to demonstrate how by using Defensive Programming you can make your API’s more robust and as a result reduce the number of bugs in your code.

If you’re a bit hazy on Defensive Programming and it’s cousins then I suggest you read the following:Defensive Programming
Fail Fast
Design by Contract (DbC)Validating method arguments for public methods should be a very simple concept to grasp and for most skilled developers is a  as natural as writing If statements. The guiding principle I follow is to trust no single input and ensure that code only executes when the supplied arguments satisfy the requirements (pre-conditio…

Load Balancing with Amazon EC2 and Elastic Load Balancing

Hosting your applications in the cloud yields many benefits, however it can be at times a very scary place when thing’s go wrong, and they always do. If you love your customers then you owe it to them to make sure you have in place redundancy and ensure you provide a high level of availability. The Amazon EC2 Infrastructure provides a really easy way to set this up using the Elastic Load Balancing feature.Before starting you need the basic concepts of how the EC2 infrastructure works and how it can help you in your quest for high availability. Regions
Regions in EC2 are geographical locations where the Data Centres reside and at the time of writing there are four Regions: US East – North Virginia US West – California EU – Ireland APAC – Singapore It goes without saying that the Region you select should be always be as close to your customers as possible.Availability ZonesEach Region has 2 or more Availability Zones. These are important to understand as they are independent from each o…

Implementing Custom Castle Windsor Facilities

If you’ve been following my posts you would know that I love Castle Windsor. One of the many useful features I have found is the Facility and I’m going to try and give a good example how you can make use of this. In a recent post I showed how you can add Cross-Cutting concerns to your application by using Interceptors.Now when configuring the Container you can explicitly configure each Interceptor per Service but when you have lot’s of components it can get pretty hard to maintain after a while and can also introduce subtle issues if someone forgets to configure it correctly.Below is how you would configure your Container without using a Facility. On the last line we are specifying the Interceptor explicitly. public void Configure() { container = new WindsorContainer(); container.Register( Component.For<CacheInterceptor>(), Component.For<ICacheProvider>() .ImplementedBy<WebCache…