Getting started with WCF
Written by Ian Elliot   
Sunday, 16 January 2011
Article Index
Getting started with WCF
Trying it out

Windows Communication Foundation (WCF) is often seen as a difficult topic. So here is a really simple hand-on introduction that shows what it is all about.

 

If you have looked at other “getting started” and “first steps in” beginner's tutorials on WCF and been lost in the first few paragraphs (or have completed the course but then not been really sure of what it was all about) don’t give up.

This getting started is different because it really is the simplest introduction to WCF imaginable. It will show you how to implement and use a service and get you started without complication and from there you can start to tinker and find out how it all works.

To make all this possible you need a copy of Visual Studio 2010. If you want to use Visual Basic then it isn't so different but the following examples all use C#.

Why so difficult?

WCF – Windows Communication Foundation - has a fearful reputation for being difficult. This isn’t undeserved and it shouldn’t be unexpected after all its doing a difficult and complex task. WCF is all about writing code that implements a service – something that you can write a client to make use of. You can see that it's already more complicated than a normal application because we have two programs to worry about – the server and the client – and we have to know about the implementation of both before anything makes sense.

To make things simple let’s just concentrate on the service side of the system and let Visual Studio provide us with a test client. We can move on to export the way a custom client is built at a later date.

A new project

Start a new C# project and select WCF Service Library – this gives you a template for a WCF basic server complete with some functionality. By examining and using the generated code you can get started with WCF very quickly.

 

Project

If you look at the project structure in the Solution Explorer you should be able to see that there are two code files – IService1.cs and Service1.cs.

 

files

 

The IService.cs file contains the definition of the service you are creating in terms of what methods it offers to the client. As the name of the file suggests, it does this by defining an Interface that lists the services methods.

The interface is marked as beginning a WCF service interface by use of the ServiceContract attribute:

 

 [ServiceContract]
 public interface IService1
 {

 

The methods that the client can use are declared as part of the interface and marked as special by the use of the  OperationsContract attribute.

In the case of the generated code there are two such methods. The first simply accepts a value as a parameter and returns a string result:

 [OperationContract]
 string GetData(int value);

This is by far the simpler of the two generated methods and it’s the one you should concentrate on for the moment.

We can return to the second method once we have seen the service do its job – so for the moment ignore details of  the  GetDataUsingDataContract method and just regard it as another method that a client could call.

Doing the work

Moving on to the Service1.cs file we encounter the code that does the actual work, i.e. it implements the interface defined in the IService.cs file.

As you would expect to do this it creates a class that inherits from the Interface – which as you know means it has to implement all of the methods defined in the Interface. However in this case Visual Studio has generated simple method implementations for us.

The GetData method simply returns the value passed to it but slightly formatted:

public class Service1 : IService1
{
 public string GetData(int value)
 {
  return string.Format(
              "You entered: {0}", value);
}
 

There is also a basic definition for GetDataUsingDataContract but we can ignore this just as we ignored its specification in the IServce.cs file as we are concentrating on the simpler of the two methods.



Last Updated ( Monday, 17 January 2011 )