Question

I am working on a simple WCF service, MiniCalcService which has only one operation Add. The client and host are both console applications. The client application takes in the operands necessary for each operation and passes them over to the service. The service returns the result which would be displayed on the client console.

  • Host is running
  • I am doing everything in code so far and there is no app.config.
  • There is no large data being passed, just two or three numbers

This worked for me yesterday. Today when I tried the same thing, it throws the following exception:

There was no endpoint listening at http://localhost:8091/MiniCalcService that could accept the message.

Here is the Stack Trace. Not that it might matter, but MiniCalcClient is developed in Visual Studio and MiniCalcService and MiniCalcHost are developed in SharpDevelop.

MiniCalcHost:

using(ServiceHost host = new ServiceHost(typeof(MiniCalcService.Service), new Uri("http://localhost:8091/MiniCalcService")))
{
    host.AddServiceEndpoint(typeof(MiniCalcService.IService),new BasicHttpBinding(),"Service");

    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    host.Description.Behaviors.Add(smb);

    host.Open();
    Console.WriteLine("Serving MiniCalcService since {0}", DateTime.Now);
    Console.Write("Press ENTER key to terminate the MiniCalcHost . . . ");
    Console.ReadKey(true);
}

MiniCalcClient:

static string Calculator(string operation, params string[] strOperands)
{
    EndpointAddress ep = new EndpointAddress("http://localhost:8091/MiniCalcService");
    IService proxy = ChannelFactory<IService>.CreateChannel(new BasicHttpBinding(), ep);

    int[] operands;
    string result = string.Empty;

    try { operands = Array.ConvertAll(strOperands, int.Parse); }
    catch (ArgumentException) { throw; }

    switch (operation)
    {
        case "add":
            result = Convert.ToString(proxy.Add(operands));//<---EXCEPTION 
            break;
        default:
            Console.WriteLine("Why was this reachable again?");
            break;
    }

    return result;
}

Service Contract IService:

[ServiceContract(Namespace="learning.wcf.MiniCalc")]
public interface IService
{
    [OperationContract]
    double Add(params int[] operands);
}

Can you please help me identify what's causing this exception?


Solution: I changed this line:

EndpointAddress ep = new EndpointAddress("http://localhost:8091/MiniCalcService");

to this:

EndpointAddress ep = new EndpointAddress("http://localhost:8091/MiniCalcService/Service");

and it worked.

Was it helpful?

Solution

I'm not sure if you can use the params in a WCF service call.... seems unnecessary, anyway....

Could you try these two service contracts instead, just to see if those would work:

[ServiceContract(Namespace="learning.wcf.MiniCalc")]
public interface IService2
{
    [OperationContract]
    int Add(int op1, int op2);
}

and

[ServiceContract(Namespace="learning.wcf.MiniCalc")]
public interface IService3
{
    [OperationContract]
    int Add(List<int> operands);
}

I'm just wondering if removing the params from your service contract might make it run - everything seems fine at first glance...


OK, so it wasn't this first attempt ......


Well - quite obvious, really: you're using a using block around the service host instantiation:

using(ServiceHost host = new ServiceHost(typeof(MiniCalcService.Service), new Uri("http://localhost:8091/MiniCalcService")))
{
    host.AddServiceEndpoint(typeof(MiniCalcService.IService),new BasicHttpBinding(),"Service");

    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    host.Description.Behaviors.Add(smb);

    host.Open();
    Console.WriteLine("Serving MiniCalcService since {0}", DateTime.Now);
    Console.Write("Press ENTER key to terminate the MiniCalcHost . . . ");
}

So by the time the code reaches the closing bracket }, the ServiceHost instance will be disposed and thus the service host closed. There's no running service host anymore!

You need to stop the code execution somewhere after the call to host.Open() by e.g.

Console.ReadLine();

or something else.

So your first claim that Host is running really doesn't hold up - it's running briefly and then is terminated again right away.....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top