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上測試過。
但我認爲在相同的網格地址上聆聽的重點是讓消息發送到該網格? – Ries 2009-10-05 13:55:04
這是真的,但假設這兩個程序運行在兩個不同的地方 - 所以一個是在機器A上聽那個地址,另一個是在機器B上。我在WCF上沒有看到任何定義兩個行爲時的行爲應用程序在同一臺計算機上的相同端點上發送和接收數據* *我懷疑WCF系統可能在您的情況下遇到了該測試案例的問題 - 因此建議。 – Streklin 2009-10-05 14:00:44
但爲什麼兩個實例都會收到自己的消息? – Ries 2009-10-06 12:58:00