我正在做一个简单的WCF服务,MiniCalcService,它只有一个操作Add。客户端和主机都是控制台应用程序。客户端应用程序接受每个操作所需的操作数,并将它们传递给服务。服务返回将显示在客户端控制台上的结果。
这个昨天对我起作用了。今天,当我尝试同样的事情时,它会抛出以下异常:
在http://localhost:8091/MiniCalcService没有能够接收消息的端点侦听。
这是堆栈跟踪。这并不重要,但MiniCalcClient是在Visual中开发的,而MiniCalcService和MiniCalcHost是用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;
}服务合同IService
[ServiceContract(Namespace="learning.wcf.MiniCalc")]
public interface IService
{
[OperationContract]
double Add(params int[] operands);
}你能帮我找出导致这个异常的原因吗?
解决方案:I更改了这一行:
EndpointAddress ep = new EndpointAddress("http://localhost:8091/MiniCalcService");对此:
EndpointAddress ep = new EndpointAddress("http://localhost:8091/MiniCalcService/Service");而且起作用了。
发布于 2012-03-26 20:31:24
我不确定您是否可以在WCF服务调用中使用params .无论如何,似乎没有必要..。
你能不能试一试这两种服务合同,看看它们是否有效:
[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实例,从而关闭服务主机。不再有运行服务主机了!
您需要在调用之后的某个地方停止代码执行。
Console.ReadLine();或者别的什么。
所以你第一次声称主机正在运行并不能维持--它只是短暂地运行,然后马上又被终止了……
https://stackoverflow.com/questions/9879239
复制相似问题