none
How do I access WCF services (running on a console application) from another computer RRS feed

  • Question

  • I have a very simple solution with 3 projects: WcfClient, WcfCommon, WcfService1:

    WcfCommon:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;
    
    namespace WcfCommon
    {
    
        [ServiceContract]
        public interface IServiceList
        {
            [OperationContract]
            void run(string id, int n);
    
            [OperationContract]
            int getResult(string id);
        }
    }

    WcfService1:

    namespace WcfService1
    {
        public class ServiceList : IServiceList
        {
            Dictionary<string, TestService> sl;
    
            public ServiceList()
            {
                sl = new Dictionary<string, TestService>();
                Console.WriteLine("ServiceList: Instantiating the service list");
            }
    
            public void run(string id, int input)
            {
                if ( !sl.ContainsKey(id) )
                {
                    sl.Add(id, new TestService(id));
                }
    
                sl[id].factorial(input);    
            }
    
            public int getResult(string id) 
            {
                if (sl.ContainsKey(id))
                {
                    Console.WriteLine("{0}--getResult: Returning {1}", id, sl[id].dec.fac);
                    return sl[id].dec.fac;
                }
                Console.WriteLine("Could not find id " + id);
                return 0;
            }
        }
    
    
    public class TestService
        {
            public Decision dec;
            public string ticker;
    
            public TestService(string id)
            {
                dec = new Decision();
                ticker = id;
                Console.WriteLine("TestService: Creating id " + id);
            }
    
            ~TestService()
            {
                Console.WriteLine("TestService: Destroying id " + ticker);
            }
    
            public int factorial(int n)
            {
                dec.fac = 1;
                for (int i = 1; i <= n; i++) dec.fac *= i;
                Console.WriteLine("{2}: Computing factorial {0}... {1}", n, dec.fac, ticker);
                return dec.fac;
            }
        }
    }
    
    

    The main Program.cs for WcfService1 is:

    namespace WcfService1
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Step 1 Create a URI to serve as the base address.
                Uri baseAddress = new Uri("http://192.168.0.5/GettingStarted/");
    
                // Step 2 Create a ServiceHost instance
                ServiceHost selfHost = new ServiceHost(typeof(ServiceList), baseAddress);
    
                try
                {
                    
    
                    // Step 4 Enable metadata exchange.
                    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                    smb.HttpGetEnabled = true;
    
                    selfHost.Description.Behaviors.Add(smb);
                    selfHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
    
                    // Step 3 Add a service endpoint.
                    selfHost.AddServiceEndpoint(typeof(IServiceList), new /*BasicHttpBinding()*/ WSHttpBinding(), "ServiceList");
                    
                    // Step 5 Start the service.
                    selfHost.Open();
                    Console.WriteLine("The service is ready.");
                    Console.WriteLine("Press <ENTER> to terminate service.");
                    Console.WriteLine();
                    Console.ReadLine();
    
                    // Close the ServiceHostBase to shutdown the service.
                    selfHost.Close();
                }
                catch (CommunicationException ce)
                {
                    Console.WriteLine("An exception occurred: {0}", ce.Message);
                    selfHost.Abort();
                }
            }
        }
    
    }
    

    The client is very simple wcfClient:

    namespace WcfClient
    
    {
        class Program
        {
            static void Main(string[] args)
            {
                wcfClientTest test = new wcfClientTest();
    
                
                for (int i = 1; i < 5; i++)
                {
                    test.testServ.run("test", i);
                    Console.WriteLine("Factorial {0}... {1}", i, test.testServ.getResult("test"));
                }
    
                for (int i = 1; i < 5; i++)
                {
                    test.testServ.run("cooc", i);
                    Console.WriteLine("Factorial {0}... {1}", i, test.testServ.getResult("cooc"));
                }
    
                Console.WriteLine("Press <ENTER> to exit client.");
                Console.ReadLine();
            }
        }
    
        public class wcfClientTest
        {
            public IServiceList testServ;
    
            public wcfClientTest()
            {
                WSHttpBinding myBinding = new WSHttpBinding();
                //BasicHttpBinding myBinding = new BasicHttpBinding();
                EndpointAddress epa = new EndpointAddress("http://192.168.0.5/GettingStarted/ServiceList");
    
                ChannelFactory<IServiceList> cf = new ChannelFactory<IServiceList>(myBinding, epa);
    
                testServ = cf.CreateChannel();
                Console.WriteLine("wcfClientTest: Instantiating a new instance");
            }
        }
    }
    

    The whole thing works well if I run both the client and the server on the same machine.

    But if I try to run the client on a different machine within the same LAN (say 192.168.0.7), the client cannot connect to the service. Any ideas?

    Thanks!

    Tuesday, November 26, 2013 12:11 PM

Answers

  • Hi,

    I am sorry for the late reply.

    Please try to check this article:
    #Calling WCF Service Across a Network:
    http://www.codeproject.com/Articles/196345/Calling-WCF-Service-Across-a-Network .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 6, 2013 10:13 AM
    Moderator
  • Hi,

    One cause might be that the server is bound to localhost and will listen only for requests from localhost.

    The other thing I will check is the firewall on your PC or network. The fact is that localhost is a loopback address so it does not go into the network when you use it. Using the actual IP of the PC goes on to the network and is routed back to the PC. So please check it.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 27, 2013 8:27 AM
    Moderator

All replies

  • Hi,

    One cause might be that the server is bound to localhost and will listen only for requests from localhost.

    The other thing I will check is the firewall on your PC or network. The fact is that localhost is a loopback address so it does not go into the network when you use it. Using the actual IP of the PC goes on to the network and is routed back to the PC. So please check it.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 27, 2013 8:27 AM
    Moderator
  • Thank you for your answer.

    I looked at the link you have on "server in bound to localhost". I do not have a tomcat / apache server running on my machine (to my knowledge). Is there another place I should be looking at?

    I am not using the local host address, but rather the LAN address of the PC. It does work if the server and client code runs on the same machine. It does not work if the code is running on another machine.

    I did allow the port through the firewall settings. I also tried to run the client / server pair with the firewall turned off.

    Any other ideas?

    Thanks!

    Thursday, November 28, 2013 2:04 PM
  • Maybe if you can refer me to a sample code that I can use that you know works would solve my problem. Thanks.
    Thursday, November 28, 2013 2:08 PM
  • Hi,

    I am sorry for the late reply.

    Please try to check this article:
    #Calling WCF Service Across a Network:
    http://www.codeproject.com/Articles/196345/Calling-WCF-Service-Across-a-Network .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 6, 2013 10:13 AM
    Moderator
  • Thank you for your answer. Went through this. It works!

    Lebneen

    Wednesday, December 11, 2013 3:26 PM