I wrote some code for a WCF P2P chat program.
<services>
<service name="PeerChat.Form1">
<host>
<baseAddresses>
<add baseAddress="net.p2p://PeerChat/" />
</baseAddresses>
</host>
<endpoint name="PeerChatEndPoint" address="" binding="netPeerTcpBinding" bindingConfiguration="BindingUnsecure"
contract="PeerChat.IChatService" />
</service>
</services>
<bindings>
<netPeerTcpBinding>
<binding name="BindingUnsecure">
<resolver mode="Pnrp" />
<security mode="None" />
</binding>
</netPeerTcpBinding>
</bindings>
<client>
<endpoint
name="PeerChatClientEndPoint"
address="net.p2p://PeerChat/"
binding="netPeerTcpBinding"
bindingConfiguration="BindingUnsecure"
contract="PeerChat.IChatService"
/>
</client>
I then host the service as follows:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public partial class Form1 : Form, IChatService
{
IChatService channel;
ServiceHost host = null;
ChannelFactory<IChatService> channelFactory = null;
private void StartService()
{
//Instantiate new ServiceHost
host = new ServiceHost(this);
//Open ServiceHost
host.Open();
//Create a ChannelFactory and load the configuration setting
channelFactory = new ChannelFactory<IChatService>("PeerChatClientEndPoint");
channel = channelFactory.CreateChannel();
//Lets others know that someone new has joined
channel.SendMessage("Hello."+ Environment.NewLine);
foreach (var cloud in Cloud.GetAvailableClouds())
{
textBox2.Text += cloud.Name + Environment.NewLine;
}
}
private void StopService()
{
if (host != null)
{
channel.SendMessage("Bye." + Environment.NewLine);
if (host.State != CommunicationState.Closed)
{
channelFactory.Close();
host.Close();
}
}
}
The problem is I can send a message to the same instance of the program but not to another instance. Ie an instance only receives its own messages not messages from other instances. Not sure if it is a matter of configuring PNRP correctly? I tested on Windows 7.