2009
07.12

So today i have been playing around with the relatively new WCF, that stands for Windows Communication Foundation.

In this post i will not try to explain the entire thing but merely give a brief example of how this tool can help you.

WCF, in short, will help you create webservices more easily:

  • WCF can keep state
  • it’s easy to create and transmit objects using JSON
  • it has AJAX integration

I’m sure there’s a lot more going on there that i am yet to discover.

So, let’s get our hands a little dirty.

I started off by creating a new WCF project (using Visual Studio 2008 ofc) and i choose for a WCF service library.

wcf-serviclibrary

Now give your new project a good name and press “Ok”.
As you can see Visual Studio will create a Service1.cs and an IService.

Before we delete those it’s smart to know what these files actually are. The IService basicly is an interface that will describe your service to the future clients. For example it will contain all your public methods!
The IService is called the contract. Your service will have to implement the contract perfectly or it wont even build.

The other file, the Service1.cs is the service itself. Here you will implement all your methods, public or not.

The contract (IService.cs)

As we take a little look in the IService file we can distinct two seperate parts; the Interface that will describe your service and a “data contract”.

In the first part, the Service Contract we will define all our public methods and we’ll give them the [OperationContract] attribute. This tag just makes clear that this is indeed a public function that future client applications will be able to call.

Int the second part, the DataContract, we can define a custom object that will be transmitted to the client applications. For example; if you write a service that will do certain operations for a bookstore you might want to create an object called “Book”.
Obviously, this is just the same thing as creating a class. All we have to do extra is define a [DataMember] attribute above each of the properties we want our object to have exposed to the clients.

What happens behind the curtains is that WCF will use this DataContract to create a JSON (or some sort) object that can be thrown to client apps.

The Service (Service.cs)

Our service class will have to implement IService ofcourse and we will have to provide all the defined methods that we listed in the contract.

Note that we can allready use our DataContract-object here.

public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.BoolValue)
{
composite.StringValue += “Suffix”;
}
return composite;
}

Just like this.

In my tryout app this is what i’ve done:

the contract:

[ServiceContract]

public interface ICarService

{

[OperationContract]

List<string> GetBrands(int depot);

}

and in the service:

public List<string> GetBrands(int depot)

{

            List brandsList = new List();
            brandsList.Add("BMW");
            brandsList.Add("Volkswagen");
            brandsList.Add("Chrysler");
            return brandsList;

}

Okay easy enough. When you run this small service a test application will start up:

wcf-test-application

When you double-click our GetBrands() method you’ll be able to invoke this method. When you do so the test application will return the brands that our service returns.

  • Share/Bookmark

3 comments so far

Add Your Comment
  1. ‘k wiste nie daj gij ne blog ad? :o

  2. :D yep

  3. [...] a previous post i explained briefly how you can make a simple wcf service. In this post i’ll try to explain how you can extend that simple service to provide an RSS [...]