One Two Three to Windows Communication Foundation: Part 1

It was a chilly Saturday night, after having a bottle or two of beer I sat down to code. It was 3 am when I looked at the clock, well I stared it for a while making sure it wasn't stopped. I have watched all the Exorcism movies and believe me, the clock stopping at 3 am isn't a good sign. Anyway, I was coding hard. Was stuck in that one code block that had taken a while. At 4 am upon the sound of the clock bell, I ran the solution for one last time to check in case of a flaw, and damn it worked.

I was working on one of my WCF services for a Windows Store Application. It was really satisfying to see how they work together in precise symphony. It requires the eyes of a programmer to see the elegance of the code for both of them, so many lines in there, yet it is all so damn clear.

In this article, I am scribbling text and code that helps the understanding WCF easily. This isn't a book, so we are not going deep into theory, rather we, with both feet, we will jump into coding.

Establishing the project

In order to create a WCF service, you need to first set up the web service project.

To begin with, open Visual Studio and create a new project.

WCF1.jpg

Since we are starting from scratch, delete the two files IService1.cs (which is an interface) and Service1.svc (which is our service).

WCF2.jpg

Now we are going to add our fresh service file, HelloWorldService.

WCF3.jpg

Once the service is added, we will have two files, one is our interface IHelloWorldService.cs and another is our service HelloWorldService.svc.

The interface acts as a contract of our service, in other words it will describe every piece of functions and classes that our service possess. That will be later used to create a WSDL document that a client downloads and reads to get a glimpse of what the linked service actually does. Here is the code of the interface.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace HelloWorld
{
    [ServiceContract]
   
public interface IHelloWorldService
    {
    [OperationContract]
       
void DoWork();
    }
}

Coding Service

In the code above, we are going to replace the function's definition void DoWork() to something like string SayHello() since we need our service to return something, and that something must be a string. This will result in the following code block:

namespace HelloWorld
{
    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string SayHello();
    }
}

Moving to the service part, in the HelloWorldServic.svc, delete the function DoWork() and implement the interface again.

WCF4.jpg

This will result in creation of a function that returns a string type. We will return a string Hello World! in this function. Here is the overall code of the service file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
 
namespace HelloWorld
{
   
public class HelloWorldService : IHelloWorldService
    {
       
public string SayHello()
        {
           
return "Hello World!";
        }
    }
}

Build the project and we are done with the service part. This is how we create services, a simple one though. In the next part we will see how to consume the service we have just created.

Happy Reading!!!