关于c#:WCF服务问题?

WCF Services issue? (2 way connection)

我有使用WCF服务的简单聊天程序。一个服务用于服务器,另一个服务用于客户端。这些服务相互连接并互相呼叫。对于托管服务器,我使用Windows服务,对于客户机,我在Windows应用程序中托管WCF服务。毕竟,我发现这个代码在简单的计算机上工作,但是当将服务器服务移动到另一台计算机时,出现了异常,服务器无法连接到客户机。我搜索并尝试其他方法。我得到了一个结果:*如果Windows应用程序中的WCF服务主机无法从其他计算机连接到它。*仅当我使用两个Windows服务(在Windows服务中承载WCF客户端服务)时,此代码才起作用。但我想知道如何在Windows应用程序中托管WCF服务,以连接和使用其他服务?这是我的密码客户端代码:管理者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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);
    }   }

客户端配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<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/聊天服务器/

服务器代码:[服务行为(InstanceContextMode=InstanceContextMode.Single)]公共类聊天服务器:ichatserver{词典客户;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    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);
    }
}

服务器配置:

1
2
3
4
5
6
7
8
9
    </binding>
  </wsHttpBinding>
</bindings>
<client>
  <endpoint
      binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_Config"
      contract="Room.IClientPoint" name="WSHttpBinding_ClientPoint">
  </endpoint>
</client>


如果您需要使用双向通信,也许您应该看看WCF双工服务。


*IF WCF SERVICE HOST IN WINDOWS APP U CAN'T CONNECT TO IT FORM ANOTHER COMPUTER

这离事实不远了。您可以检查以下几点:

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