WCf service problem?

Nov 19, 2009 at 7:46 AM

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>

  </system.serviceModel>

  <applicationSettings>

    <Room.Properties.Settings>

      <setting name="ClientPointAddress" serializeAs="String">

        <value>http://PChost:8731/ClientPoint/</value>

      </setting>

      <setting name="ChatServerAddress" serializeAs="String">

        <value>http://PCserver:8731/ChatServer/</value>

      </setting>

    </Room.Properties.Settings>

  </applicationSettings>

</configuration>

 

 

Server  code:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

    public class ChatServer : IChatServer

    {

        Dictionary<string, ClientInvoker> 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:

<system.serviceModel>

    <services>

      <service behaviorConfiguration="Host.Service1Behavior" name="Host.ChatServer">

        <endpoint address="" binding="wsHttpBinding" contract="Host.IChatServer" bindingConfiguration="WSHttpBinding_Config">

        </endpoint>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

        <host>

          <baseAddresses>

            <add baseAddress="http://PCserver:8731/ChatServer/" />

          </baseAddresses>

        </host>

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="Host.Service1Behavior">

          <!-- To avoid disclosing metadata information,

          set the value below to false and remove the metadata endpoint above before deployment -->

          <serviceMetadata httpGetEnabled="True"/>

          <!-- To receive exception details in faults for debugging purposes,

          set the value below to true.  Set to false before deployment

          to avoid disclosing exception information -->

          <serviceDebug includeExceptionDetailInFaults="False" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <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="Room.IClientPoint" name="WSHttpBinding_ClientPoint">

      </endpoint>

    </client>

  </system.serviceModel>