Extending the CustomPeerResolverService: LinearResolverService.cs

For background information on PeerChannel discovery mechanisms and options please refer to https://msdn2.microsoft.com/en-us/library/aa702771.aspx.

By default, PeerChannel uses the Windows Peer Name Resolution Protocol (PNRP) for discovery of peers and neighbors in the mesh. But we also provide an alternative server-based out-of-the-box discovery service- the CustomPeerResolverService- for situations/platforms where PNRP is not available or feasible. This post talks about how to go about extending default implementation of the custom resolver, for greater control over mesh shaping and discovery.

In general, when a Peer node first starts up, it tries to look up new neighbors by invoking the CustomPeerResolverService.Resolve() method before going ahead and registering itself with the resolver service. To control the shape of your mesh, for example, all you would need is an extension to the resolver service which is similar to default implementation, except for custom Resolve(), Register(), Unregister() methods. In actual programming terms, it means that your customResolverSerivce class implementation would simply inherit from our base implementation and then have scenario-specific customizations and overrides.

Sample User Scenario: I want to create a linear mesh (i.e. every node is connected to exactly one neighbor; the custom name resolver should therefore provide the new requesting node with referrals to only the last node to register with it).

Solution: Attached to the post is a simple extension to the default CustomPeerResolverService with overridden Resolve(), Register(), Unregister() methods.

Configuring your PeerChannel client application to use your custom service: This can be done by configuring the instance of NetPeerTcpBinding in your application like so: 

<snip>

NetPeerTcpBinding binding = new NetPeerTcpBinding();

customResolverEpr =

"net.tcp://" + customResolverHostName + ":" + CustomResolverServicePort.ToString() + "/DefaultResolverService";

binding.Resolver.Mode =

PeerResolverMode.Custom;

binding.Resolver.Custom.Address =

new EndpointAddress(customResolverEpr);

binding.Resolver.Custom.Binding =

new NetTcpBinding(SecurityMode.None);

</snip>

LinearResolverService.cs