2009-10-05 53 views
1

我为WCF P2P聊天程序写了一些代码。WCF点对点聊天

<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> 

然后我承载服务如下:

[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(); 
      } 
     } 
    } 

的问题是,我可以将消息发送到该程序的同一个实例而不是到另一个实例。即一个实例只接收自己的消息而不是来自其他实例的消息。不知道这是否正确配置PNRP?我在Windows 7上测试过。

回答

1

你不会碰巧有两个程序实例都在听同一个终点吗?我不确定,但我怀疑可能发生的情况是您的客户端应用程序首先在端点上注册自己,然后在第二个端点获得它们之前拦截到该端点的所有消息。我建议试图做的是将第二个实例配置为在具有不同Uri的端点上启动。所以说一个连接net.p2p:// PeerChatA /和另一个net.p2p:// PeerChatB /。

+0

但我认为在相同的网格地址上聆听的重点是让消息发送到该网格? – Ries 2009-10-05 13:55:04

+0

这是真的,但假设这两个程序运行在两个不同的地方 - 所以一个是在机器A上听那个地址,另一个是在机器B上。我在WCF上没有看到任何定义两个行为时的行为应用程序在同一台计算机上的相同端点上发送和接收数据* *我怀疑WCF系统可能在您的情况下遇到了该测试案例的问题 - 因此建议。 – Streklin 2009-10-05 14:00:44

+0

但为什么两个实例都会收到自己的消息? – Ries 2009-10-06 12:58:00