Click here to Skip to main content
Click here to Skip to main content
Go to top

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

Introduction

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.

Background

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.

[ServiceContract]
public interface ICustomers
{
    [OperationContract]
    string GetRandomCustomerName();
    [OperationContract]
    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;
    }

    #endregion
}

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)))
    {
        host.Open();
        Console.WriteLine("Server Started");
        Console.ReadLine();
        host.Close();
    }
}

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" ?>
<CONFIGURATION>
    <SYSTEM.SERVICEMODEL>
        <SERVICES>
            <SERVICE name="Server.Customers">
                <ENDPOINT contract="CustomersInterface.ICustomers" 
            binding="netTcpBinding" 
            address="net.tcp://localhost:5555/Customers" />
            </SERVICE>
        </SERVICES>
    </SYSTEM.SERVICEMODEL>
</CONFIGURATION>

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();
        Console.WriteLine(name);
        Console.WriteLine(count);
    }
}

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" ?>
<CONFIGURATION>
    <SYSTEM.SERVICEMODEL>
        <CLIENT>
            <ENDPOINT name="MyClient" 
        contract="CustomersInterface.ICustomers" 
        binding="netTcpBinding" 
        address="net.tcp://localhost:5555/Customers" />
        </CLIENT>
    </SYSTEM.SERVICEMODEL>
</CONFIGURATION>         

License

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

Share

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

 
GeneralNew to WCF Pinmemberblackkongu2-Apr-08 19:34 

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 | Mobile
Web04 | 2.8.140926.1 | Last Updated 18 Jun 2006
Article Copyright 2006 by Ehab El-Gindy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid