dotnettechy.com Your Ad Here

Wednesday, September 9, 2009

WCF Chat Sample


The Chat sample demonstrates how to implement a multiparty chat application by using PeerChannel. Messages sent by any instance of a chat application are received by all other instances.

To get samples and instructions for installing them

  • Do one or more of the following:
    • On the Help menu, click Samples.
      The Readme displays information about samples.
    • Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.
    • Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.
For more information, see Locating Sample Files.
Bb690929.alert_security(en-us,VS.90).gifSecurity Note:
This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To run the sample

  1. In Solution Explorer, open the .sln file.
    Whenever a step refers to a client or a service, those steps apply to separate instances of the sample (because the Chat sample is not a client and service application).
  2. Press F5 to run the Chat application.
  3. Start as many instances (named instance and located in Visual Basic installation path\Projects\Chat\Instance\Bin\Debug\Instance.exe) as you want. Start by entering a nickname that will distinguish messages sent from a particular client instance. Shortly after this name is entered, chat messages can be sent to the mesh. These messages should be sent to all the other instances that have a distinct member name. (That is, a message from a client that has the same name will not be displayed, and a single client's own message will not be sent to the console window of that client.)

The Chat sample is not a client and service application. It is a true peer-to-peer application, in which each instance acts as a peer of other instances. Each instance can send messages to other instances and receive messages from other instances by using the IChat duplex contract.

PeerChannel is a multiparty, peer-to-peer (P2P) communication technology in Windows Communication Foundation (WCF). It helps to enable secure, scalable, and reliable messaging. One common example of a multiparty application that can benefit from PeerChannel is a collaborative application such as chat. In chat, a group of people communicate with one another in a peer-to-peer manner without servers. PeerChannel enables P2P collaboration, content distribution, load balancing, and distributed processing for both consumer and enterprise scenarios.
PeerChannel introduces the following new concepts:
  • A mesh is a named collection (an interconnected graph) of peer nodes that can communicate among themselves and that are each identified by a unique mesh ID.
    Bb690929.alert_note(en-us,VS.90).gifNote:
    Active nodes in the mesh publish their mesh names so that others can find them. A mesh adjusts to changing membership, it has adaptable connectivity in an environment in which nodes are constantly joining and leaving the mesh, and it is dynamically optimized to adapt to traffic patterns.
  • Active nodes in the mesh publish their mesh names so that others can find them. A mesh adjusts to changing membership, it has adaptable connectivity in an environment in which nodes are constantly joining and leaving the mesh, and it is dynamically optimized based on traffic patterns.
  • A peer node is an endpoint in a mesh. A single application can have multiple peer nodes that participate in different meshes.
  • A peer resolver is responsible for resolving a mesh ID to the endpoint addresses of the nodes in the mesh. A peer node uses these addresses to connect to other nodes in the mesh. This enables messages to be propagated throughout the mesh.
Chat is a Windows Forms application. Each instance of a Chat application creates an IDuplexChannel that has the same endpoint address. Because all instances of a Chat application use the same address, a message that is sent by one instance of the application is received by all other instances on its peer channel.
The Chat application defines and implements the IChat duplex contract. The IChat contract allows for only one-way operations because the ServiceModel does not support the single-request/multiple-response paradigm. (In a multiparty channel, a single request that is sent to the mesh can generate multiple responses.)
This sample implements a static Main function to create an IClientChannel with the IChat duplex contract. The function uses the endpoint that is specified in the configuration file.
All Chat instances must use the same endpoint address to ensure that the messages sent by one instance are received by all other instances.


Chat instances in this sample locate each other either by using a custom resolver or by using the default peer resolver (PNRP). Note that PNRP is not available on Windows Server 2003. Therefore, a custom resolver must be used to run this sample on a system that is running Windows Server 2003. By default, this sample is set up to use a custom resolver. Whether a custom resolver or the default resolver is used is determined by the chat endpoint defined in the following configuration file. To switch to the default peer resolver (PNRP), replace BindingCustomResolver with BindingDefault under bindingConfiguration in the sample's configuration file.



                   address="net.p2p://chatMesh/ServiceModelSamples/Chat" 
                   binding="netPeerTcpBinding" 
                   bindingConfiguration="BindingCustomResolver" 
                   contract="Microsoft.ServiceModel.Samples.IChat">
         
To enable the peer node to communicate with the Peer Channel Custom Peer Resolver service, the client-side configuration of the Peer Channel Custom Peer Resolver is defined in the configuration file.





address="net.tcp://localhost/ServiceModelsamples/peerResolverService"
    binding="netTcpBinding"
    bindingConfiguration="Binding3"
    contract="Microsoft.ServiceModel.SamplesICustomPeerResolver">


The address identifies the address of the resolver service. If the resolver service is running on a remote computer, replace localhost with a qualified domain name.

1 comment:

jashua-newway said...

Great post thanks! Meanwhile, I found some useful comet ajax library for ASP.NET with WCF support. http://pokein.codeplex.com