C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Client server sample using WCF and net.tcp binding


Posted Date: 30 Jun 2008    Resource Type: Articles    Category: .NET Framework
Author: Tony JohnMember Level: Diamond    
Rating: 1 out of 5Points: 20



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:

Handling custom SOAP headers via WCF Behaviors

Attachments

  • WCF_NET_TCP_Client_Server_Sample (19314-301840-WCF_SimpleClientServer_net_tcp.zip)



  • Responses

    Author: Sajid P K    18 Jul 2008Member Level: Gold   Points : 1
    hii... wht is its main differnce from remoting. is it same as remoting ?


    Author: beena    28 Jul 2008Member Level: Silver   Points : 0
    Sharepoint-Webparts


    SharePoint2003 Development - Building
    Author: Happy Coder    30 Jul 2008Member Level: Gold   Points : 0
    Good One


    Author: Arun Kumar Pandey    07 Aug 2008Member 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: avtar    18 Aug 2008Member 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: gopi    09 Sep 2008Member Level: Silver   Points : 1
    hi TONY

    gud one

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



    Author: Gaurav Arora    18 Oct 2008Member Level: Diamond   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 Iyer    17 Nov 2008Member Level: Diamond   Points : 1
    Hi Tony,

    Excellent article.

    Anyone could gain good knowledge on WCF through this.

    Thanks a lot.

    Regards,
    Pradeep Y


    Author: sanjay kumar    11 Dec 2008Member Level: Bronze   Points : 0
    Very well described about wcf


    Author: Shuby Arora    02 Feb 2009Member Level: Gold   Points : 1
    Hi,

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


    keep smilling
    Shuby arora


    Author: forcrack    27 Feb 2009Member 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 srivastava    12 Mar 2009Member Level: Bronze   Points : 0
    Gud article.


    Author: Uttam Dhakal    26 Mar 2009Member 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 Kant    08 Apr 2009Member Level: Silver   Points : 1
    Go with following link:

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



    Author: Dosth Mohammed    12 Apr 2009Member Level: Bronze   Points : 1
    here you can get more step by step tutorial from microsoft

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


    Author: aradhya    12 May 2009Member Level: Silver   Points : 1
    Hi,
    Nice Article i got lot a Good idea regarding WCF.
    Productive stuff.


    Author: Gaurav    13 May 2009Member Level: Silver   Points : 0
    Please explain the scenarios where WCF can be used effectively.


    Author: Praveen Pula    26 May 2009Member Level: Gold   Points : 0
    Nice one


    Author: Kirankumar Potnuru    08 Jun 2009Member Level: Gold   Points : 1
    Its Good.
    Can you explain what is the diffrence among WCF ,webservice,windows service and remoting


    Author: Prasoon Kumar    16 Jun 2009Member Level: Silver   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: Shunmuganathan M    17 Jun 2009Member Level: Diamond   Points : 0
    Nice article...



    Author: Kirankumar Potnuru    18 Jun 2009Member Level: Gold   Points : 0
    Hi Prasoon Kumar ,
    Thanks for giving valuble information


    Author: Ramesh Sahu    03 Sep 2009Member Level: Gold   Points : 0
    Good For Develpope and testing.


    Author: muthuraman    07 Sep 2009Member Level: Silver   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


    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    (No tags found.)

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Data Validation Controls in ASP.NET 2.0 Part 1
    Previous Resource: Reflection in C#
    Return to Discussion Resource Index
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use