Configuring Peer Channel: With/Without App.config

So now that we know what the NetPeerTcpBinding is how you can end up using Peer Channel in your standard WCF application, let's take a look at how to configure peer channel. We follow the standard WCF (.NET really) pattern of allowing configuration through an App.config file for your application or imperatively within the code itself. Which way you choose to go really depends on your needs - if you expect your endpoints/configuration to change a lot and want that aspect flexible etc(eg you use a customResolver for discovery and expect to move it around etc) you'd want to minimize code changes and prefer to change these settings in your config file directly. On the other hand, if you dont really expect to modify your source code that often and dont really want an App.config file, you can opt to just configure everything in code itself. Peer Channel (NetPeerTcpbinding) supports both, to provide a programming model consistent with the rest of WCF.

Using App.config:

The following example attempts to configure a Peer Channel endpoint, with endpointAddress = "net.p2p://ChatMesh/chat"; other endpoint information also is added to the endpoint section of the config file, including fully qualified contract type that is associated with the endpoint, name of config file section that configures the specific binding used, and of course also the type of the binding being used.

All binding configuration (in our case, peer channel (NetPeerTcpbinding)-specific ) settings are in the configuration section corresponding to the one specified in the endpoint section above. (Note: Just in case you are wondering, Visual Studio 8 comes preconfigured with xml information for ServiceModel so intellisense would work and you dont really have to know before hand all the various properties etc.. a quick way to confirm is to check your schema file from the VS install here: C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas\DotNetConfig.xsd and search for serviceModel definitions..)

In the bindings section of serviceModel configuration, we specify settings for NetPeerTcpBinding as i show below.

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.serviceModel>

<client>

<!-- chat instance participating in the mesh -->

<endpoint name="PeerEndpoint"

address="net.p2p://ChatMesh/chat"

binding="netPeerTcpBinding"

bindingConfiguration="PeerBinding"

contract="Microsoft.ServiceModel.Samples.IChat">

</endpoint>

</client>

<bindings>

<netPeerTcpBinding>

<binding name="PeerBinding" port="0">

<resolver mode="Auto"/>

<security mode="None" />

        </binding>

      </netPeerTcpBinding>

    </bindings>

 

  </system.serviceModel>

</configuration>

It all comes together when in the code, you specify the endpoint name from the config file to the CreateChannel constructor, like so:

// Construct InstanceContext to handle messages on callback interface.

// An instance of ChatApp is created and passed to the InstanceContext.

InstanceContext site =

new InstanceContext(new ChatApp());

// Create the participant with the given endpoint configuration. Each participant opens a duplex channel to the mesh. Participant is an instance of the chat application that has opened a channel to the mesh

ChannelFactory<IChatChannel> factory = new DuplexChannelFactory<IChatChannel>(site, "PeerEndpoint");

Note: Notice that the above example assumes that you are creating a duplexChannel for your application and the client itself is the sender/receiver. You can certainly choose to configure your application such that instead of a duplex channel, you have 1 separate sender and multiple clients (receivers). Check out the Default sample for an idea on how to configure the service/client separately in such a case.

 

Imperative configuration (Via code and not through the App.config file):

 

The same configuration above can be achieved through setting the properties on NetPeerTcpbinding programmatically. So programmatically, the settings above would look as under:

  

InstanceContext site = new InstanceContext(new ChatApp());

NetPeerTcpBinding peerBinding = new NetPeerTcpBinding();

peerBinding.Security.Mode = SecurityMode.None;

ChannelFactory<IChatChannel> factory = new DuplexChannelFactory<IChatChannel>(site, peerBinding);    

 

That's all there is to it :)- (unless of course you would like to use the default custom resolver, in which case check out the scenario sample on msdn on what properties to configure). Hopefully this will help answer some common questions around configuration and also how to convert your app from using App.config to pure code.

 

As always, feedback welcome and appreciated :)..

 

-shalini.