English 中文(简体)
WCF 服务问题 (二) 连接
原标题:WCF Services issue? (2 way connection)
  • 时间:2009-11-19 08:38:12
  •  标签:
  • c#
  • .net
  • wcf

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);
    }   }

客户会议:

<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/

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 
");
            File.AppendAllText(@"c:ServiceChatLog.txt", e.ToString() + " 
");
        }
        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>

最佳回答

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

问题回答

* 附件不译。

从真相来看,这还可能更进一步。 你可以检查几个方面:

  • The server s firewall -- you re using a non-standard port, 8731, are you sure it s allowed?
  • The address -- can you connect to that IP and Port from the client normally? Try using telnet or putty, or expose the WSDL on the server and hit it through a browser.
  • 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?




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签