【问题标题】:WCF Services issue? (2 way connection)WCF 服务问题? (2路连接)
【发布时间】:2010-12-18 05:18:01
【问题描述】:

我有一个使用 WCF 服务的简单聊天程序。一种服务用于服务器,另一种用于客户端。这些服务相互连接并相互调用。对于托管服务器,我使用了 Windows 服务,对于客户端,我在 Windows 应用程序中托管 WCF 服务。毕竟我发现这段代码可以在简单的计算机上运行,​​但是当将服务器服务移动到另一台计算机时,会引发异常并且服务器无法连接到客户端。我搜索并尝试了其他方法。 我得到一个结果: *如果 WCF 服务主机在 WINDOWS 应用程序中,您无法从另一台计算机连接到它。 *此代码仅在我使用两个 WINDOWS 服务时才有效(在 Windows 服务中托管 WCF 客户端服务) 但我想知道如何在可以连接和使用其他服务的 Windows 应用程序中托管 WCF 服务? 这是我的代码 客户端代码: 管理器.cs

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/

服务器代码: [服务行为(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>

【问题讨论】:

    标签: c# .net wcf


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      *如果 WINDOWS 应用程序中的 WCF 服务主机无法从另一台计算机连接到它

      这与事实相去甚远。您可以检查几件事:

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

      【讨论】:

        猜你喜欢
        • 2016-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-11
        • 2011-05-04
        • 1970-01-01
        • 2010-10-11
        • 2010-10-10
        相关资源
        最近更新 更多