Click here to Skip to main content
11,414,619 members (69,168 online)
Click here to Skip to main content

WCF Basic Client/Server

, 18 Jun 2006
Rate this:
Please Sign up or sign in to vote.
An article on how to build a client/server application using WCF


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, Binding and Contract (ABC).

The 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.

The contract specifies the interface between the client and server, it's simply a class interface with some attributes.

The 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 (GetRandomCustomerName,GetSCustomersCount).
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 contract.

public interface ICustomers
    string GetRandomCustomerName();
    int GetCustomersCount();

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()
        return 100;


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)))
        Console.WriteLine("Server Started");

Now the server is configured to run using the ICustomers 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 Address and Binding as follows:

<?xml version="1.0" encoding="utf-8" ?>
            <SERVICE name="Server.Customers">
                <ENDPOINT contract="CustomersInterface.ICustomers" 
            address="net.tcp://localhost:5555/Customers" />

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 =
                new ChannelFactory<ICUSTOMERS>("MyClient"))
        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" ?>
            <ENDPOINT name="MyClient" 
        address="net.tcp://localhost:5555/Customers" />


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Ehab El-Gindy
Web Developer
Egypt Egypt
My name is Ehab El-Gindy,working as teaching assistant in the faculty of computers and information, Helwan University, Egypt.

Holding MCAD, MCT and Microsoft Goto Trainer for Vista and WinFX (.Net 3.0)

Comments and Discussions

Questionhow to programmatically add HTTP binding? Pin
vhrao21-Mar-13 14:32
membervhrao21-Mar-13 14:32 
Questionmy vote is 5 Pin
anharal7-Dec-12 23:51
memberanharal7-Dec-12 23:51 
QuestionGreat Job! Pin
ub_prabhakant1-Mar-12 7:19
memberub_prabhakant1-Mar-12 7:19 
Thanks for a simple and yet powerful demo of WCF. Frequently, people put lots of fluffy code around simple concepts and that confuses the main issue. Your example is to the point. Please keep it up, and post similarly intuitive findings that you make.

Well done!
Question3Tier, WCF Pin
Member 232129330-Sep-09 0:09
memberMember 232129330-Sep-09 0:09 
AnswerRe: 3Tier, WCF Pin
Ehab El-Gindy30-Sep-09 3:14
memberEhab El-Gindy30-Sep-09 3:14 
GeneralThank You! Pin
merlin98126-Sep-08 11:05
membermerlin98126-Sep-08 11:05 
GeneralNew to WCF Pin
blackkongu2-Apr-08 20:34
memberblackkongu2-Apr-08 20:34 
Generala request from an unknown user Pin
Sean Ewington1-Jun-07 10:00
adminSean Ewington1-Jun-07 10:00 
GeneralNow can make the above aplication Pin
hussain.patel6-Oct-06 10:37
memberhussain.patel6-Oct-06 10:37 
QuestionCan not make WCF application Pin
MyLoveDataBase25-Jun-06 7:03
memberMyLoveDataBase25-Jun-06 7:03 
AnswerRe: Can not make WCF application Pin
Ehab El-Gindy25-Jun-06 8:16
memberEhab El-Gindy25-Jun-06 8:16 
AnswerRe: Can not make WCF application Pin
jswoofer13-Nov-06 4:45
memberjswoofer13-Nov-06 4:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.4 | Last Updated 18 Jun 2006
Article Copyright 2006 by Ehab El-Gindy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid