Tuesday, April 1, 2008

ServiceHost vs InstanceContext

Well, I might be missing something but from what I can tell using ServiceHost doesn't make very much sense in PeerChannel application. From my experiments if you create a ServiceHost and then create a DuplexChannel, when you invoke a service operation on the channel the message actual appears to arrive twice. Also creating a 'regular' Channel (ChannelFactory instead of DuplexChannelFactory) doesn't push the message to all nodes – it seems like only some of the nodes actually receive the message. There are a few examples out there using a 'regular' Channel – but when I run more that than 3 nodes using that configuration each node just does not get the message.

So – What I do recommend is using InstanceContext housed in a separate thread and DuplexChannels for all sending of data. A quick sample might look like this…

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Reentrant,
UseSynchronizationContext = false)]
public class ServiceImplementation: IPeerServiceContract
{ /* snip */ }

public static class WorkerThread
{
/* snip variables */
public static void Run()
{
service = new ServiceImplementation();
host = new InstanceContext(service);
host.Open();


channelFactory = new
DuplexChannelFactory(service,
"WCFPeerPerformance");
channelFactory.Credentials.Peer.MeshPassword = MeshPassword;
channel = channelFactory.CreateChannel();
channel.Open();
}
}

No comments: