我正在开发一个简单的 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");
它奏效了。