WCF服务peoblem?(2路连接) [英] WCF Services peoblem?(2 way connection)

查看:98
本文介绍了WCF服务peoblem?(2路连接)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用WCF服务的简单的聊天程序。一个服务使用的服务器,另一个用于客户端。这些服务相互连接并相互调用。对于托管服务器,我用了一个窗口服务和客户端我在一个Windows应用程序托管WCF服务。毕竟我发现,简单的计算机此code的工作,但是当移动服务器服务到另一台计算机的异常升高,服务器无法连接到客户端。我搜索并尝试其他方式。 我得到的结果: *如果WCF服务主机Windows应用程序 - 可以不连接到它形成另一个计算机。 *本code保护正常工作,只有当我(在Windows服务托管WCF客户端服务)使用了两个Windows服务 但我想知道如何托管WCF服务在Windows应用程序,可以与其它服务连接和工作? 这是我的code 客户端code: Manager.cs

I have simple chat program using WCF service. 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. 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. *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? This is my code Client code: 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/

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

Server code: [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class ChatServer : IChatServer { Dictionary clients;

    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双面打印服务

If you need to use 2-way communication, maybe you should take a look at WCF Duplex Services.

这篇关于WCF服务peoblem?(2路连接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆