Peer to Peer Series Part 5: Connecting Peers via WCF

10/8/2009 11:48:00 AM

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:

View the Screencast:

Download the Demo Code

Tags:

Peer2Peer

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

About the author

Jeff Brand Jeff Brand

This is the personal web site of Jeff Brand, self-proclaimed .NET Sex Symbol and All-Around Good guy. Content from my presentations, blog, and links to other useful .NET information can all be found here.

E-mail me Send mail


Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

My Twitter Updates

XBOX
Live

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012

Sign in