Click here to Skip to main content
15,905,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
have a very simple solution with 3 projects: WcfClient, WcfCommon, WcfService1:

WcfCommon:


C#
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:
C#
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:

C#
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:

C#
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!
Posted
Comments
ZurdoDev 26-Nov-13 7:44am    
Yes, you need to update the web.config to point to the service.
Member 10359335 26-Nov-13 7:57am    
Thanks

I don't have any config files, tried to do everything in code. What do you mean by web.config? Where is it located? Do I need it if my server is a console application?
ZurdoDev 26-Nov-13 7:59am    
The reason Microsoft made WCF as an "upgrade" to regular webservices was to move all of the configuration out of code and into a config file for easier maintenance. You'll want to use a web.config instead of hardcoding where the service is.
Member 10359335 26-Nov-13 8:04am    
Ok, understood. What would you change in the code above to make this work with web.config. Or do you have a sample that I can look at?

Thanks!
ZurdoDev 26-Nov-13 8:06am    
I would refer you to MSDN for lots of samples. I have not actually done WCF in several years so things may have changed a little.

But, when you add your reference you do it through Add Service Reference in visual studio and that will end up adding the endpoint into your web.config so that when you deploy and the address changes it is much easier to update.

1 solution

If the WCF service has been hosted on the IIS server.... then u need to hit the IP address of that machine with the port no. on which it has been hosted in the URL.....


Hope, this will gives you some idea....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900