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
hii... wht is its main differnce from remoting. is it same as remoting ?