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)
{
Uri baseAddress = new Uri("http://192.168.0.5/GettingStarted/");
ServiceHost selfHost = new ServiceHost(typeof(ServiceList), baseAddress);
try
{
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
selfHost.AddServiceEndpoint(typeof(IServiceList), new WSHttpBinding(), "ServiceList");
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
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();
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!