AJAX enabled WCF Service and ASP.NET


A friend of mine has recently asked me some questions about ASP.NET AJAX and I decided that it would be a good idea to explore this, and publish it as a blog post. I whipped up a simple application that uses ASP.NET Ajax web form and it consumes services from a AJAX-enabled WCF Service. It’s fairly straightforward, so I’m going to dive straight into the implementation details, and some problems I encountered. I’ve made use of the data grid control from an open source library called Ajax Data Controls for this sample because it’s been designed for client side scripting and is more intuitive to use compared to the ASP.NET ones. Of course this could have been easily done with JQuery, but I wanted to try something different.

wcf_ajax Read the rest of this entry »

Advertisements

Tip: Use specific port number when hosting WCF service in Cassini Web Server


When you are hosting a WCF service in Visual Studio, a Cassini Web Server will spin up and assign a random port number, e.g. http://localhost:61447/MyService. If you add a Service Reference, it will use that port number currently assigned by Cassini. This is all good until you stop the Cassini Web Server, or restart VS IDE. Where did my old port number go???

Read the rest of this entry »

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 »

How to host and consume WCF RESTful Services


There’s been a lot of jazz about RESTful services, which was published to the masses by a person named Roy Thomas Fielding in his PhD dissertation. Since then, RESTful services have been embraced and widely used. To name a few, Twitter and Facebook exposes RESTful web services API to us to consume. I shall not go into the principles and concepts of REST, but I personally find RESTful services to be very natural, easy to understand and it just flows with how the World Wide Web works, which is driven by the HTTP protocol. You can learn more about RESTful principles from wikipedia, or just google it. It’s very much the opposite of using SOAP, which has been the Microsoft way for some time now, but in .NET 3.5, REST has found its way into WCF.

Now back to business, let’s talk about how to create a RESTful service using WCF. If you search google, you will find heaps of examples how to do create WCF RESTful services, but there’s been very little information out there about how to consume it. I found some examples about using WCF ChannelFactory and generating a client proxy to consume the WCF RESTful service, but I feel these approaches violates the whole point of using REST. In order to consume a RESTful service, regardless of how it’s hosted, we should just consume it using simple HTTP requests generated from code. In this post, I will show you how to create, host and consume the services with WCF and the .NET framework.

Read the rest of this entry »

Posted in WCF. Tags: . 5 Comments »

Decorator Pattern with WCF and Castle Windsor


How do you extend a WCF Service that you cannot modify? Or even if you can modify it, should you? Consider the Open Closed Principle. Assuming that you can’t modify the existing service, but want to leverage existing service somehow from your new service, what should we do? That’s where the Decorator Pattern comes into the picture.

The Decorator Pattern is commonly used to modify or add additional behaviour to a class dynamically without modifying it’s current behavior.

Assuming we have an existing service, like so.

Read the rest of this entry »