2009-11-19 87 views
0

我有使用WCF服务的简单聊天程序。一个服务用于服务器,另一个用于客户端。这些服务彼此连接并互相呼叫。对于托管服务器,我使用了Windows服务,对于客户端,我在Windows应用程序中托管WCF服务。毕竟我发现这个代码可以在简单的计算机上工作,但是当将服务器服务移动到另一台计算机时引发异常并且服务器无法连接到客户端。我搜索并尝试其他方式。 我得到一个结果: *如果WCF服务主机在WINDOWS应用程序U无法连接到它形成另一台计算机。 *此代码仅在我使用两个窗口服务时运行(在Windows服务中托管WCF客户端服务) 但是我想知道如何在Windows应用程序中托管WCF服务,可以连接并使用其他服务? 这是我的代码 客户端代码: Manager.csWCF服务问题? (2路连接)

public delegate void UserInfoHandeler(string UserName); 
public delegate void MessageHandeler(string Message); 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
public class Manager : IClientPoint 
{ 
    public void SendUserList(string[] users) 
    { 
     frmRoom.Members = users; // this method called by Server (WCF service which host in windows service) 
     //when server call this method I have an exception with SSPI 
    } 
    public void SendMessage(string message) 
    { 
     frmRoom.ReciveMessage = message; // this method called by Server (WCF service which host in windows service) 
     //when server call this method I have an exception with SSPI 

    } 

    FrmJoin frmJoin; 
    FrmRoom frmRoom; 
    ChatServerClient ServiceInvoker; 

    public string User 
    { 
     get; 
     set; 
    } 

    public void Run() 
    { 
     frmJoin = new FrmJoin(); 
     frmJoin.LoginEvent += new UserInfoHandeler(frmJoin_LoginEvent); 
     ServiceInvoker = new ChatServerClient("WSHttpBinding_ChatServer", Settings.Default.ChatServerAddress); 
     frmJoin.ShowDialog(); 
    } 

    void frmJoin_LoginEvent(string UserName) 
    { 
     frmRoom = new FrmRoom(); 
     frmRoom.SendMessageEvent += new MessageHandeler(frmRoom_SendMessageEvent); 
     frmJoin.LogoutEvent += new UserInfoHandeler(frmJoin_LogoutEvent); 
     User = UserName; 
     frmRoom.ReciveMessage = ServiceInvoker.Login(User, Settings.Default.ClientPointAddress); 
     frmRoom.ShowDialog(); 
    } 

    void frmJoin_LogoutEvent(string UserName) 
    { 
     string message = ServiceInvoker.Logout(UserName, Settings.Default.ChatServerAddress); 
    } 

    void frmRoom_SendMessageEvent(string Message) 
    { 
     ServiceInvoker.SendMessage(User, Message); 
    } } 

客户端配置:

<system.serviceModel> 
<bindings> 
    <wsHttpBinding> 
    <binding name="WSHttpBinding_Config" closeTimeout="00:05:00" 
    openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" 
    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
    maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" 
    messageEncoding="Mtom" textEncoding="utf-8" useDefaultWebProxy="true" 
    allowCookies="false"> 
     <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
     maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
     <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> 
     <security mode="Message"> 
     <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> 
     <message clientCredentialType="Windows" negotiateServiceCredential="true" 
     algorithmSuite="Default" establishSecurityContext="true" /> 
     </security> 
    </binding> 
    <binding name="MyConfig" closeTimeout="00:10:00" openTimeout="00:10:00" 
       sendTimeout="00:10:00" maxReceivedMessageSize="2147483647"> 
     <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
     maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
    </binding> 
    </wsHttpBinding> 
</bindings> 
<client> 
    <endpoint 
     binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_Config" 
     contract="Host.IChatServer" name="WSHttpBinding_ChatServer"> 
    </endpoint> 
</client> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="Room.Service1Behavior"> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="false" /> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<services> 
    <service behaviorConfiguration="Room.Service1Behavior" name="Room.Manager"> 
    <endpoint address="" binding="wsHttpBinding" contract="Room.IClientPoint" bindingConfiguration="WSHttpBinding_Config"> 
    </endpoint> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    </service> 
</services> 

http://PChost:8731/ClientPoint/ http://PCserver:8731/ChatServer/

服务器代码: [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 公共类的ChatServer:IChatServer { 字典客户;

public ChatServer() 
    { 
     clients = new Dictionary<string, ClientInvoker>(); 
    } 

    public string Login(string Username, string address) 
    { 
     try 
     { 
      ClientInvoker client = new ClientInvoker("WSHttpBinding_ClientPoint", address); 
      clients.Add(Username, client); 
      foreach (ClientInvoker clientinvoker in clients.Values) 
       clientinvoker.SendUserList(clients.Keys.ToArray()); 
     } 
     catch (Exception e) 
     { 
      File.AppendAllText(@"c:\ServiceChatLog.txt", "Service trow Exeption \n"); 
      File.AppendAllText(@"c:\ServiceChatLog.txt", e.ToString() + " \n"); 
     } 
     return string.Format("Welcom {0}", Username); 
    } 

    public string[] GetListUser() 
    { 
     return clients.Keys.ToArray(); 
    } 

    public void SendMessage(string userName, string ReciveMessage) 
    { 
     string message = string.Format("{0} : {1}", userName, ReciveMessage); 
     foreach (ClientInvoker clientinvoker in clients.Values) 
      clientinvoker.SendMessage(message); 
    } 
    public string Logout(string Username, string address) 
    { 
     clients.Remove(Username); 
     foreach (ClientInvoker clientinvoker in clients.Values) 
     { 
      clientinvoker.SendUserList(clients.Keys.ToArray()); 
      clientinvoker.SendMessage(string.Format("{0} left ROOM", Username)); 
     } 
     return string.Format("Godbye {0}", Username); 
    } 
} 

服务器配置:

</binding> 
    </wsHttpBinding> 
</bindings> 
<client> 
    <endpoint 
     binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_Config" 
     contract="Room.IClientPoint" name="WSHttpBinding_ClientPoint"> 
    </endpoint> 
</client> 

回答

2

如果需要使用双向通信,也许你应该看一看WCF Duplex Services

+0

感谢您的帮助 – Rev 2009-11-24 05:37:36

+0

好文章就在这里 http://www.codeproject.com/KB/WCF/WCFDuplexReentrant.aspx – Rev 2010-05-11 05:33:55

0

*如果Windows中的APPüWCF服务主机无法连接到它形成另一个计算机

这不能从事实并非如此。你可以检查几件事:

  • 服务器的防火墙 - 你使用的是非标准端口8731,你确定它是允许的吗?
  • 地址 - 你能正常连接到客户端的IP和端口吗?尝试使用telnet或putty,或者在服务器上公开WSDL并通过浏览器打开它。
  • 安全性 - 客户端端点正在使用Windows身份验证 - 两台计算机位于同一个域还是与两台服务器上配置的用户相同?