This article shows how to build a client/server application using Windows Communication Foundation (WCF) which is a part of .NET 3.0 (WinFX). WCF makes life easier for developers to build connected applications.
Most of the applications now require to support the SOA architecture. This means that the application should provide a service interface to be exposed to other applications, these interfaces can be exposed using many technologies, i.e. ASP.NET, XML Web Services, COM+ and MSMQ and so on. Each technology is completely different from the other technologies and requires special knowledge of how to code it.
Now WCF makes life better, developers will use WCF to expose service interfaces using any technology. WCF has the same set of functions and attributes that will be used for all technologies (bindings) which will abstract more implementation details and will improve productivity.
The developer should simply identify three major points before starting work with WCF:
address specifies the location of the service which will be exposed like http://myserver/myservice. Clients will use this address to communicate with the service.
contract specifies the interface between the client and server, it's simply a class interface with some attributes.
binding specifies how the two parties will communicate in terms of transport and encoding and protocols.
Using the code
First of all we will developer the
contract, which is a simple interface class (
ICustomers) containing two functions (
In order to declare that the interface is a contract "
ServiceContract" attribute is used. We will also use the "
OperationContract" attribute for each operation that we want to expose in the
public interface ICustomers
Second we will develop the Server. The server should implement the
contract simply by implementing the
ICustomers interface as the following code:
public class Customers : ICustomers
#region ICustomers Members
public string GetRandomCustomerName()
return "Random Name";
public int GetCustomersCount()
After this, we will develop the core server side code which starts the server:
static void Main(string args)
using (ServiceHost host = new ServiceHost(typeof(Customers)))
Now the server is configured to run using the
Contract, but we didn't specify the
Address or the
Binding. It's a good practice to add them in a config file outside the source code so we can modify them later.
The App.Config file contains the
Binding as follows:
This file can be generated using the SvcConfigEditor.exe tool under "\Program Files\Microsoft SDKs\Windows\v1.0\Bin"
Now we are done, the server is completed and we can start building the client. The client will call the two methods of the
contract in its main function as following:
static void Main(string args)
using (ChannelFactory<ICUSTOMERS> customersFactory =
ICustomers customersProxy = customersFactory.CreateChannel();
string name = customersProxy.GetRandomCustomerName();
int count = customersProxy.GetCustomersCount();
Now we can generate the App.config of the client in the same way that we generated the server with the SvcConfigEditor.exe to get the following file:
<?xml version="1.0" encoding="utf-8" ?>