Peer Channel Custom Peer Resolver

Download sample

The Custom Peer Resolver sample demonstrates how to implement a custom resolver that can be used with peer channel applications.

This sample is based on the Self-Host sample. Also, refer to the Getting Started Sample for a high-level overview of Windows Communication Foundation (WCF).


The setup procedure and build instructions for this sample are located at the end of this topic.

Key concepts:

A peer channel is a multiparty, peer-to-peer (P2P) communication technology in WCF. It provides a secure and scalable message-based P2P communication channel for application developers. One common example of a multiparty application that can benefit from the peer channel is collaborative applications such as chat, where a group of people chat with one other in a peer to peer manner without requiring servers. A peer channel enables both consumer and enterprise scenarios around P2P collaboration, content distribution, load balancing, and distributed processing.

Peer channel 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 identified by a unique mesh ID.


    Active nodes in the mesh publish their mesh names, so others can find them. A mesh has the following characteristics: It adjusts to changing membership, it has resilient 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. An application can have multiple peer nodes that participate in different meshes.

  • A peer channel is a channel that is constructed using the netPeerTcpBinding binding, or a custom binding that uses the PeerTransportBindingElement.

  • A peer resolver is responsible for resolving a mesh ID to the endpoint addresses of the nodes in the mesh. When a peer node is opened, it uses a peer resolver to resolve the mesh ID to a list of addresses of other peer nodes in the mesh. This creates a mesh of interconnected nodes that enables messages to be propagated throughout the mesh.

The sample demonstrates how to write a custom peer resolver Web service and to self-host the service from within an executable. The client is not a stand-alone application, but is compiled with other peer channel applications that use a custom peer resolver. See the Peer Channel Chat sample to see how peer channel applications can use the client side of a custom peer resolver.

A custom peer resolver service is a singleton service that caches the mesh IDs and the endpoint addresses in a dictionary and responds to registration, un-registration, and resolves requests from the clients.

Mesh IDs are expected to be unique. If multiple applications use the same resolver, they should choose different mesh IDs to avoid conflict.

The sample implements a static main function to create a ServiceHost for the given CustomPeerResolverService type. The host is also responsible for providing a base address to the service host, which has been configured in the application settings in the configuration file (App.config).

    <!-- use appSetting to configure base address provided by host -->
    <add key="baseAddress"
     value=" net.tcp://localhost/servicemodelsamples/peerResolverService" />

The service implements the contract, which exposes the RegisterMeshId, UnregisterMeshId, and ResolveMeshId operations. The client makes synchronous requests to a given operation, and the service replies with the result.

The service exposes a single endpoint for communicating with the service that is defined using the configuration file (App.config). The binding is configured with a standard NetTcpBinding, which provides TCP communication.

       service="Microsoft.ServiceModel.Samples. CustomPeerResolverService">
       <!-- use base address provided by the host -->
       <endpoint address=""
            binding=" netTcpBinding "
             contract="Microsoft.ServiceModel.Samples. ICustomPeerResolver" />

As configured, the service can be accessed at net.tcp://localhost/servicemodelsamples/peerResolverservice by a client on the same machine. For clients on remote machines to access the service, a qualified domain name must be specified instead of localhost.

When you run the sample, the operation registration and un-registration messages are displayed in the service console window. If multiple registrations are made for the same mesh ID, only the first registration and the last un-registration cause messages to be displayed in the console window. Press ENTER in the console window to shut down the service (after shutting down the peer channel applications using this service).


This 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, please follow the correct exception handling best practices.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. To build the C#, C++, or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single-machine or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples. (The Peer Channel Chat sample application also includes its own implementation of the Custom Peer Resolver. Please follow the instruction on the Peer Channel Chat page to build and run the Customer Resolver and the Peer Chat instances.)

© 2007 Microsoft Corporation. All rights reserved.