Peer to Peer Series Part 5: Direct Connect Peers via WCF
- Posted: Oct 08, 2009 at 10:51 AM
- 11,872 Views
Download
How do I download the videos?
- To download, right click the file type you would like and pick “Save target as…” or “Save link as…”
Why should I download videos from Channel9?
- It's an easy way to save the videos you like locally.
- You can save the videos in order to watch them offline.
- If all you want is to hear the audio, you can download the MP3!
Which version should I choose?
- If you want to view the video on your PC, Xbox or Media Center, download the High Quality WMV file (this is the highest quality version we have available).
- If you'd like a lower bitrate version, to reduce the download time or cost, then choose the Medium Quality WMV file.
- If you have a Zune, WP7, iPhone, iPad, or iPod device, choose the low or medium MP4 file.
- If you just want to hear the audio of the video, choose the MP3 file.
Right click “Save as…”
- WMV (WMV Video)
- MP3 (Audio only)
- MP4 (iPod, Zune HD)
- Mid Quality WMV (Lo-band, Mobile)
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 http://slickthought.net
Comments Closed
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.