Client server sample using WCF and net.tcp binding

Introduction



This sample demonstrates how to use WCF (Windows Communication Foundation) to develop a typical client server application using TCP/IP binding.
You may refer to several tutorials to get an introduction to WCF. The purpose of this article is to demonstrate a sample implementation of client
server application using WCF and TCP/IP binding.

About this WCF Sample



This sample is a typical client server application, which includes a client that need to login first before it can access other components on the server.

The client application uses a login screen where user can input user id and password. When you login, the user id and password will be validated by calling corresponding
services on the server. Once authenticated, an authentication token will be returned from the server. Client stores this token in a custom context on the client.

After authenticating, the authentication token is sent from the client to the server as a custom Header along with each request made to the server. Server
retrieves this token from the headers and validates against a custom database.

This sample includes 2 solutions:

1. WCF Host
2. WCF Client

As the name indicates, the first solution represents a windows applications which hosts the WCF server components.

The second solution represents the client application which communicates with the server.

The Server project has 2 important manager classes:

1. UserManager - This class provides the services to authenticate user on the server side using user id and password. Also, returns a custom token to the client so that client need to pass only this authentication for any service calls.
2. SomeOtherManager - This class represents any other services that you want to implement on the server side. In this WCF client server example, this manager class implements only one method - GetServerTime(). But this method validates to see if user is authenticated before returning the server time.

ClientMessageInspector

This class implements the interface IClientMessageInspector and is used to inspect the messages from the client side.
In our example, we are adding some custom headers to the request using this class. We are sending the authentication
token as part of the header to the server. Server retrieves this token and validates the calls.

ServerMessageInspector

This class implements the interface IDispatchMessageInspector and is used on the server side to inspect the incoming requests.

CustomBehavior

CustomBehavior class implements the interface IEndpointBehavior and it is used to add the custom message inspectors to the WCF system.

How it works ?

The client application first shows the login screen. If user successfully login, an Authentication token is returned by the server. The client stores
this token using a custom class called ClientContext. From this point onwards, the ClientmessageInspector class retrieves this token and
sends to the server as a custom header along with each request.

On the server side, service methods explicitly checks if the authentication token is passed in the header and verifies it is valid or not.

References

The purpose of this article is to provide a simple client server sample application that uses WCF with net.tcp binding. I have not explained how to use custom behaviours to
inspect the messages on the client and server side, even though I have used them here to inject the headers to the requests and responses.

I learned it from Paolo Pialorsi's blog:

http://weblogs.asp.net/paolopia/archive/2008/02/25/handling-custom-soap-headers-via-wcf-behaviors.aspx


Attachments

  • WCF .NET TCP Client/Server Sample (19314-301840-WCF_SimpleClientServer_net_tcp.zip)
  • Comments

    Author: Sajid P K18 Jul 2008 Member Level: Gold   Points : 1

    hii... wht is its main differnce from remoting. is it same as remoting ?

    Author: beena28 Jul 2008 Member Level: Silver   Points : 0

    Sharepoint-Webparts

    Author: Happy Coder30 Jul 2008 Member Level: Gold   Points : 0

    Good One

    Author: Arun Kumar Pandey07 Aug 2008 Member Level: Silver   Points : 1

    WCF is not same as remoting.Remoting works only on Microsoft plateform.
    WCF is a unification technology which unites the following technologies:-

    .NET remoting
    MSMQ
    Web services
    COM+.

    Author: avtar18 Aug 2008 Member Level: Gold   Points : 2

    WCF is more loosely coupled inshort in remoting your hosting that is low level protocol TCPIP and your logic are tightly coupled where as in WCF you write your logic once and host it as webservice , remote service ..etc

    so if u see in earlier version if same logic u want to host as webservice over http and also as remote serive over tcpip , you have to re-write everything but in wcf its just write once and host it on any binding..

    Author: gopi09 Sep 2008 Member Level: Silver   Points : 1

    hi TONY

    gud one

    but how to set " your resource to 1st one allways " in the list

    Author: Gaurav Kumar Arora18 Oct 2008 Member Level: Gold   Points : 1

    Hi Tony,

    A very good introductory Article for WCF. What is merely difference here from Remoting where using low-level protocols.

    Could you please suggest a Book for the same topic.

    Thanks & regards,
    Gaurav Arora

    Author: Pradeep Iyer17 Nov 2008 Member Level: Gold   Points : 1

    Hi Tony,

    Excellent article.

    Anyone could gain good knowledge on WCF through this.

    Thanks a lot.

    Regards,
    Pradeep Y

    Author: sanjay kumar11 Dec 2008 Member Level: Bronze   Points : 0

    Very well described about wcf

    Author: Shuby Arora02 Feb 2009 Member Level: Gold   Points : 1

    Hi,

    Excellent article.
    We'll gain good knowledge through this.


    keep smilling
    Shuby arora

    Author: forcrack27 Feb 2009 Member Level: Bronze   Points : 2

    http://infoworld202.blogspot.com

    Processing form data with PHP

    Create WebSite

    Add meta tag to blog website

    Python Tutorial

    databases in Perl

    Using Databases In PHP

    Boot Sector

    Creating A Simple Search Engine In PHP

    Assembly language Tutorial

    Batch file programming

    free Mircoprocessors Tutorial Training

    C Socket Programming In Unix

    http://infoworld202.blogspot.com

    Author: Akanksha srivastava12 Mar 2009 Member Level: Bronze   Points : 0

    Gud article.

    Author: Uttam Dhakal26 Mar 2009 Member Level: Silver   Points : 1

    .net remoting morever can be thinked as application to application communication whereas WCF is a bundle of communication technology which supports not only application to application but also machine to machine

    Author: Surya Kant08 Apr 2009 Member Level: Silver   Points : 1

    Go with following link:

    http://www.4guysfromrolla.com/articles/031204-1.aspx

    Author: Dosth Mohammed12 Apr 2009 Member Level: Silver   Points : 1

    here you can get more step by step tutorial from microsoft

    http://msdn.microsoft.com/en-us/library/ms734712.aspx

    Author: aradhya12 May 2009 Member Level: Silver   Points : 1

    Hi,
    Nice Article i got lot a Good idea regarding WCF.
    Productive stuff.

    Author: Gaurav13 May 2009 Member Level: Silver   Points : 0

    Please explain the scenarios where WCF can be used effectively.

    Author: Praveen Pula26 May 2009 Member Level: Gold   Points : 0

    Nice one

    Author: Kirankumar Potnuru08 Jun 2009 Member Level: Gold   Points : 1

    Its Good.
    Can you explain what is the diffrence among WCF ,webservice,windows service and remoting

    Author: Prasoon Kumar16 Jun 2009 Member Level: Gold   Points : 2

    @ Kirankumar Potnuru :-
    WebServices :- It can be defined as a service which can be

    utilized by an Application.

    You create a Webservice when you need to share same information

    between different Applications which may be/may be not developed

    in
    same technology(therefore it gives Flexibility of developing

    Application with the help of different Technology) and may be

    running on Different Platforms.

    One classic example is stock Web services,since Any Application

    developed in dotnet/java/others may like to have this

    information about a stock ,therefore these informations has be

    developed like something which is sharable by any

    Application.In this scenario we will like to develop this

    as Webservice.

    Remoting :-Remoting also serves the same purpose as Webservice

    but it doesn't have flexibility of supporting heterogeneous

    environment but only serves purpose in homogeneous

    environment.

    Author: Nathan17 Jun 2009 Member Level: Gold   Points : 0

    Nice article...

    Author: Kirankumar Potnuru18 Jun 2009 Member Level: Gold   Points : 0

    Hi Prasoon Kumar ,
    Thanks for giving valuble information

    Author: Ramesh Sahu03 Sep 2009 Member Level: Gold   Points : 0

    Good For Develpope and testing.

    Author: muthuraman07 Sep 2009 Member Level: Gold   Points : 1

    hi tony,
    this is a wonderful article.
    we will gain very good knowledge on WCF through this one.
    thanks,

    with warm regards,
    muthuraman

    Author: nishithraj24 Nov 2009 Member Level: Gold   Points : 1

    Tony,

    Nice article.

    One question.

    How the platform independency is achieved in WCF. Whether the client & Server calls are through SOAP??

    Author: sajjad ahmad26 Nov 2009 Member Level: Bronze   Points : 1

    Its a simple but very knowledgeful article on WCF.
    Thanx buddy.

    How to Host WCF service in IIS?

    Author: satyendra kr. Pandit02 Mar 2010 Member Level: Silver   Points : 0

    http://msdn.microsoft.com/en-us/library/ee354381.aspx

    Author: YuDi03 Mar 2010 Member Level: Gold   Points : 0

    hi tony
    i dont know why we are using wcf sevices
    we can use simplw window services instead of wcf services

    thanks

    Author: Naveen Kumar17 Mar 2010 Member Level: Gold   Points : 2

    WCF is more loosely coupled inshort in remoting your hosting that is low level protocol TCPIP and your logic are tightly coupled where as in WCF you write your logic once and host it as webservice , remote service..

    so if u see in earlier version if same logic u want to host as webservice over http and also as remote serive over tcpip , you have to re-write everything but in wcf its just write once and host it on any binding..

    Author: Raju Melveetilpurayil24 May 2010 Member Level: Gold   Points : 2

    Respected MVP,
    After reading this article i dont understand how it works? i download that attachment file help me a lot to understand what is WCF am poor in .net Remoting, WSE and MSMQ. i just read that WCF is .net Remoting, WSE and MSMQ. can i implement Chat program using WCF?. if you read my comment please suggest me and i will search relevant article abt WCF.

    Author: Vamshikrishna Reddy24 Aug 2010 Member Level: Gold   Points : 0

    Nice Article

    Author: arunms25 Aug 2010 Member Level: Gold   Points : 1

    Thanks Tony...Nice article..

    WCF is a combined features of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication.


    Author: praveen09 Sep 2010 Member Level: Gold   Points : 0

    Nice...

    Author: Ilia15 Sep 2010 Member Level: Bronze   Points : 0

    Works great thanks.

    Author: mike22 Nov 2010 Member Level: Bronze   Points : 1

    Hi, Great post.

    Been trying to add a Callback functionality to this example but new to WCF so was wondering if anyone could give a example using Tony John´s example on how to use a callback that would fire of say 2 minutes after the user logged in replying some simple text string ?.

    Author: saritagupta14 Feb 2011 Member Level: Bronze   Points : 1

    Hi,
    Nice post :)..

    In classic object or component- oriented programming model offered only single way for client to call a method. Client will issue a call, block while the call was in progress, and continue executing once the method returned.

    WCF will support classical Request-Replay model, along with that it also supports One-Way call(call and forget operation) and callback(service to call back the client)

    Three modes of communication between client and service are

    Request- Replay
    One-Way
    Callback

    Author: PR Sydney02 Jun 2013 Member Level: Bronze   Points : 5

    Hi Tony, thanks for a wonderful article. I am learner and working on VB.NET. I converted your code into VB.NET using SharpDevelop but it doesn't work as it should.

    I have now both versions (C# and VB.NET) of the code and below combinations of the code working/not-working (I get "You are not authenticated") in not working combination:

    working: c# host and C# client
    working: C# host and vb client
    working: vb host and c# client
    not working: vb host and vb client

    can someone please have a look and suggest me what is wrong in vb.net code. I have spend 2 days and checked entire code many time but still no luck.

    I don't know C# so want to use VB.NET.

    thanks in advance.

    Don't know how/where to attach the code, so could someone help and I will send the code by email.

    Author: Laxmikant12 Jun 2013 Member Level: Gold   Points : 4

    Nice Article...
    Just to add here ... NetTcpBinding is mostly used when you have clients only in network, netNamedPipeBinding is used when the clients are on the same machine.
    wsHttpBinindg or basicHttpBinding is used if clients can call it through http protocols like website.
    In real application you have to implement same service with multiple bindings.
    Here is my article on handling multiple wcf services.

    http://dotnetmentors.com/multiple-wcf-services-in-one-solution-with-multiple-endpoints-and-hosting.aspx