Peer to Peer Series Part 5: Direct Connect Peers via WCF

Play Peer to Peer Series Part 5: Direct Connect Peers via WCF
Sign in to queue


In Part 5 of the Peer to Peer Series, I show a sample application that uses PNRP to connect a client application to a server application using WCF and PNRP Peer Host Names. The details of the actual WCF Service implementation.  Instead, I will focus on those areas of leveraging PNRP to establish connections via WCF.

The video will show a sample application that is comprised of a client and a server application.  You could use a more “peer-to-peer” duplex-style of communication, but the client-server arrangement makes the two key parts of establishing a WCF connection easier to explain.

When hosting a WCF service, the key code from the IntelServiceHost.cs file is shown below:


this.PeerRegistration.Start(peerClassifier, port);
Uri tcpUri = new Uri(string.Format("net.tcp://{0}:{1}/IntelService", this.PeerRegistration.PeerUri, port));
_serviceHost = new ServiceHost(service, tcpUri);
NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);
_serviceHost.AddServiceEndpoint(typeof(IIntelService), tcpBinding, "");

The PeerRegistration property exposes a wrapper class around the PNRP API.  The Start method registers the application with a given peer classifier and port as an unsecure peer name in the Global PNRP Cloud.  The PeerUri property passes back the peerhostname.  That uri is used to setup a new Uri, along with the port, that is then used to configure the ServiceHost using a NetTcpBinding.  Some of this could have been configured in the application’s config file, but I put it all here so you can see how the different parts relate.

The client side of the equation is even easier (found in the IntelClient.cs file):

PeerNameResult peerRecord = PeerResolution.ResolveHostName(hostPeerName);
System.ServiceModel.Channels.Binding netBinding = new NetTcpBinding(SecurityMode.None);
EndpointAddress endpointAddress = new EndpointAddress(string.Format("net.tcp://{0}:{1}/IntelService", peerRecord.Uri, peerRecord.Port));
IntelServiceProxy = new IntelProxy(netBinding, endpointAddress);

The PeerResolution class is another wrapper around the PNRP API, this time providing resolving a given unsecure peer name in the Global PNRP cloud.  It returns an object containing the remote peer’s peerhostname and port. The rest of the code is just setting up the client side of the WCF connection.

Other documentation for PNRP:

Find the demo code at



Download this episode

The Discussion

Add Your 2 Cents