Introduction
Before a WCF Service can be consumed by a client application, the following three steps are required:
Service: The actual WCF Service which provides business functionality
Host: A hosting environment which hosts the service
Client: A client application/service which invokes the WCF Service
In the following sections, we will create a solution which consists of three separate projects representing the service, host and the client respectively. I will be using Visual Studio 2008 to create the application.
Getting Started
1. Start a new instance of Visual Studio 2008.
2. Click on File -> New Project. From Project types pane, select Visual Studio Solutions
3. From the Template pane, select Blank Solution
4. Enter WCFIntroduction for Project Name. Also specify the location according to you working preference
This will create a blank solution as shown in figure 1. Let us now create the actual WCF Service.
Fig 1
Creating the Service
1. In the solution explorer, right click on ‘WCFIntroduction’ and select Add -> New Project.
2. Select Class Library template and name it as WCFCalculator. Make sure that the location is set to ..\WCFIntroduction as shown in figure 2. Click OK to create the project.
Fig 2
3. The newly created project contains a single file ‘Class1.cs’. Rename it to ‘WCFCalculatorService.cs’. We will return to this file shortly.
4. From the solution explorer, right click on ‘WCFCalculator’ project and select ‘Add Reference’. The ‘Add Reference’ dialog appears. From the .NET tab, select ‘System.ServiceModel’ and click OK. A reference to the respective namespace is added which is required to work with WCF Services
5. Again right click on ‘WCFCalculator’ project and select Add -> New Item. Select ‘Class’ template, name it as ‘IWCFCalculatorService.cs’ as shown in figure 3 and click Add
6. This will create a new class file ready to be coded
Fig 3
7. Change the class definition to ‘public interface IWCFCalculatorService’. Type in the code as shown in listing 1 and save the file
Listing 1
using System;
using System.ServiceModel;
namespace WCFCalculator
{
[ServiceContract(Namespace = "http://youruniquedomain")]
public interface IWCFCalculatorService
{
[OperationContract]
int AddNumbers (int x, int y);
}
}
8. Let us now return to the ‘WCFCalculatorService.cs’ file. Open it and type in the following code:
Listing 2
using System;
using System.ServiceModel;
namespace WCFCalculator
{
public class WCFCalculatorService : IWCFCalculatorService
{
public int AddNumbers (int x, int y)
{
return x + y;
}
}
}
Compile this project to make sure there are no errors. We have just created a very basic WCF Service used to add two numbers. The next step is to host this service in a hosting environment. In the following section, we will create a windows console application which will host this service.
Hosting the Service
1. Right click on the solution ‘WCFIntroduction’ and select Add -> New Project.
2. Select ‘Console Application’ template, name it as ‘Host’ as shown in figure 4 and click OK
Fig 4
3. Right click on ‘Host’ project and select Add Reference. From the .NET tab, select System.ServiceModel. From the Projects tab, select ‘WCFCalculator’ and press OK
4. The Host project consists of one file ‘Program.cs’. Open this file and type in the code as shown in listing 3
Listing 3
using System;
using System.ServiceModel;
namespace Host
{
class Program
{
static void Main (string[] args)
{
using (ServiceHost host = new ServiceHost (typeof (WCFCalculator.WCFCalculatorService), new Uri ("http://localhost:8000/WCFCalculator")))
{
host.AddServiceEndpoint (typeof (WCFCalculator.IWCFCalculatorService), new BasicHttpBinding (), "WCFCalculatorService");
host.Open ();
Console.WriteLine ("Pressto terminate service...");
Console.ReadLine ();
}
}
}
}
5. Compile this project. Make sure that the Host project is set as Startup Project (You can do so by right clicking on Host project and select ‘Set as Startup Project’). Press Ctrl+F5 to run this project. You should see a command window with message ‘Press
Fig 5
Consuming the Service – Client Application
1. Right click on solution ‘WCFIntroduction’ and select Add -> New Project
2. Select ‘Console Application’ template, name it as Client and click OK as shown in figure 6
Fig 6
3. Add a reference to System.ServiceModel namespace (as done in the above steps)
4. This project consists of a single file ‘Program.cs’. Open this file and type in the code as shown in listing 4
Listing 4
using System;
using System.ServiceModel;
namespace Client
{
class Program
{
static void Main (string[] args)
{
EndpointAddress endPoint;
IWCFCalculatorService proxy;
int result;
endPoint = new EndpointAddress ("http://localhost:8000/WCFCalculator/WCFCalculatorService");
proxy = ChannelFactory.CreateChannel (new BasicHttpBinding (), endPoint);
result = proxy.AddNumbers (2, 3);
Console.WriteLine ("2 + 3 = " + result.ToString ());
Console.WriteLine ("Pressto terminate client...");
Console.ReadLine ();
}
}
}
5. Here is the tricky part. In the above code, a proxy is created using the Service Contract (IWCFCalculatorService). To use this interface, copy it over from the ‘WCFCalculator’ project to the ‘Client’ project. Don’t forget to change the namespace to ‘Client’ in this file (In short the same interface now exists in both the ‘WCFCalculator’ and ‘Client’ project).
6. Compile the ‘Client’ project and make it the Startup Project (Right click on ‘Client’ project and select ‘Set as Startup Project’). Press Ctrl+F5 and you should see the output as shown in figure 7
Fig 7
Wow, you just create a WCF service from scratch and got it working. Though it may not be simple, I am sure it’s worth a try.
How it Works
Let me explain some of the concepts discussed in the above sections. If you have read my previous post, many of the details should be self explanatory. In listing 1, the IWCFCalculatorService interface is the Service Contract since it is decorated with the [ServiceContract] attribute. The single method within this interface - AddNumbers – is a WCF method as its decorated with the [OperationContract] attribute. Since IWCFCalculatorService is a service contract, any class implementing this interface becomes a WCF Service. This is also true for the WCFCalculatorService class as shown in listing 2.
In listing 3, an instance of ServiceHost class is created. This class provides a host for a WCF Service. It accepts two parameters, Type and params respectively. The Type parameter specifies the type (WCFCalculator.WCFCalculatorService) of the service while params specifies a collection of addresses (http://localhost:8000/WCFCalculator) where the service can be hosted. Next the ServiceHost defines a new Endpoint by calling the AddServiceEndPoint (). This method accepts two parameters including a contract (WCFCalculator.IWCFCalculatorService) and the type of binding (basicHttp) used by the Endpoint. Once an endpoint is defined, the service is open and available to accept requests from the client.
For a client to communicate with the service, a proxy is required. In listing 4, the generic ChannelFactory
Summary
In this post, I walked you through creating your first WCF Service. You may find it a bit difficult in the beginning but as you gain experience, WCF is a great tool to work with. In future post, I will talk more about WCF. So stay tuned for more…