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 Visual Studio Samples.
Security 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
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).
Press F5 to run the Chat application.
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.)
Demonstrates
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.
Key Concepts
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.
Note
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.
<!-- Chat instance participating in the mesh. -->
<endpoint name="ChatEndpoint"
address="net.p2p://chatMesh/ServiceModelSamples/Chat"
binding="netPeerTcpBinding"
bindingConfiguration="BindingCustomResolver"
contract="Microsoft.ServiceModel.Samples.IChat">
</endpoint>
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.
<!-- Client used to communicate with the custom resolver service. -->
<client>
<endpoint configurationName="CustomPeerResolverEndpoint"
address="net.tcp://localhost/ServiceModelsamples/peerResolverService"
binding="netTcpBinding"
bindingConfiguration="Binding3"
contract="Microsoft.ServiceModel.SamplesICustomPeerResolver">
</endpoint>
</client>
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.
The sample also demonstrates how to retrieve the peer node from IClientChannel and how to register for online and offline events by using IOnlineStatus. An online event is initiated when the peer node is connected to at least one other peer node in the mesh. An offline event is initiated when the peer node is no longer connected to any other peer node in the mesh.
Currently, metadata cannot be generated because a peer channel is not integrated with the service metadata utility (Svcutil.exe).
When you run the sample, the chat messages sent by a Chat instance are displayed in the console windows of other Chat instances. Press the Q key followed by ENTER in each console window to close the instance.
Note
The sample currently does not handle all possible exceptions that the infrastructure may throw. If you are using these samples in a commercial or production environment, follow the appropriate exception-handling best practices.