简体   繁体   English

WCF服务问题? (2路连接)

[英]WCF Services issue? (2 way connection)

I have simple chat program using WCF service. 我有使用WCF服务的简单聊天程序。 One service use for server and another use for client. 一种服务用于服务器,另一种用于客户端。 Those services connect to each other and call each other. 这些服务相互连接并互相呼叫。 For hosting server, I used a windows service and for client I host WCF service in a Windows app. 对于托管服务器,我使用Windows服务,对于客户端,我在Windows应用程序中托管WCF服务。 After all I found that this code work on simple computer, but when move server service to another computer an exception raised and server can't connect to the client. 毕竟,我发现此代码可在简单的计算机上运行,​​但是将服务器服务移至另一台计算机时会引发异常,并且服务器无法连接到客户端。 I searched and try other ways. 我搜索并尝试其他方式。 I get a result: *IF WCF SERVICE HOST IN WINDOWS APP U CAN'T CONNECT TO IT FORM ANOTHER COMPUTER. 我得到一个结果:*如果WCF服务宿主在WINDOWS APP中,则您无法通过另一台计算机连接到它。 *THIS CODE WORKED ONLY WHEN I USED TWO WINDOWS SERVICES (hosting WCF client service in a windows service) But I want to know HOW hosting WCF service in windows app that can connect and work with another services? *仅当我使用两个WINDOWS服务(在Windows服务中托管WCF客户端服务)时,此代码才起作用,但是我想知道如何在Windows应用程序中托管WCF服务,该服务可以连接并与其他服务一起使用? This is my code Client code: Manager.cs 这是我的代码客户端代码:Manager.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);
    }   }

Client config: 客户端配置:

<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/ http:// PChost:8731 / ClientPoint / http:// PCserver:8731 / ChatServer /

Server code: [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class ChatServer : IChatServer { Dictionary clients; 服务器代码:[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);
    }
}

Server config: 服务器配置:

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

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

*IF WCF SERVICE HOST IN WINDOWS APP U CAN'T CONNECT TO IT FORM ANOTHER COMPUTER *如果WINDOWS APP中的WCF服务主机无法通过另一台计算机连接到它

This couldn't be further from the truth. 这离事实还远。 You can check a few things: 您可以检查以下几件事:

  • The server's firewall -- you're using a non-standard port, 8731, are you sure it's allowed? 服务器的防火墙-您使用的是非标准端口8731,确定可以使用该端口吗?
  • The address -- can you connect to that IP and Port from the client normally? 地址-您可以正常从客户端连接到该IP和端口吗? Try using telnet or putty, or expose the WSDL on the server and hit it through a browser. 尝试使用telnet或腻子,或在服务器上公开WSDL并通过浏览器访问它。
  • Security -- the client endpoint is using Windows authentication -- are the two machines on the same domain or is the same user configured on both servers? 安全性-客户端端点使用Windows身份验证-两台计算机是在同一域中还是在两台服务器上配置了同一用户?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM