0

我正在开发一个简单的 WCF 服务,MiniCalcService它只有一个操作Add。客户端和主机都是控制台应用程序。客户端应用程序接收每个操作所需的操作数并将它们传递给服务。该服务返回将显示在客户端控制台上的结果。

  • 主机正在运行
  • 到目前为止,我都在代码中做所有事情,并且没有 app.config。
  • 没有传递大数据,只有两三个数字

这昨天对我有用。今天当我尝试同样的事情时,它抛出了以下异常:

http://localhost:8091/MiniCalcService上没有可以接受消息的端点侦听。

这是堆栈跟踪。没关系,但它MiniCalcClient是在 Visual Studio中开发的,MiniCalcService并且MiniCalcHost是在 SharpDevelop 中开发的。

迷你计算器主机

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);
}

迷你计算器客户端:

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;
}

服务合同 IService

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

您能帮我确定导致此异常的原因吗?


解决方案:我改变了这一行:

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

对此:

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

它奏效了。

4

1 回答 1

2

我不确定您是否可以params在 WCF 服务调用中使用...。无论如何,这似乎是不必要的...。

你可以试试这两个服务合同,看看它们是否可行:

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

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

我只是想知道params从您的服务合同中删除是否可以使其运行 - 乍一看一切似乎都很好......


好吧,所以这不是第一次尝试......


嗯 - 非常明显,真的:你在using服务主机实例化周围使用了一个块:

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 . . . ");
}

因此,当代码到达右括号}时,ServiceHost实例将被释放,从而服务主机关闭。不再有正在运行的服务主机!

您需要在调用 by 之后在某处停止代码执行host.Open(),例如

Console.ReadLine();

或者是其他东西。

因此,您第一次声称Host 正在运行真的站不住脚——它只是短暂地运行,然后立即再次终止.....

于 2012-03-26T20:31:24.260 回答