遇到了这样一个问题,谁能解释一下是什么回事?
WCF宿主程序是一个控制台程序,代码如下
static void Main(string[] args)
{
//HostingServiceViaCode();
using (ServiceHost calculatorServiceHost = new ServiceHost(typeof(CalculatorService)))
{
calculatorServiceHost.Opened += delegate { Console.WriteLine("Calculator Service begin to listen via the Address:{0}", calculatorServiceHost.BaseAddresses[0].ToString()); };
calculatorServiceHost.Open();
Console.Read();
}
}
运行宿主程序,开启服务。运行客户端程序,正常。
可当我把Console.Read();移到Using语句的外面,如下:
static void Main(string[] args)
{
//HostingServiceViaCode();
using (ServiceHost calculatorServiceHost = new ServiceHost(typeof(CalculatorService)))
{
calculatorServiceHost.Opened += delegate { Console.WriteLine("Calculator Service begin to listen via the Address:{0}", calculatorServiceHost.BaseAddresses[0].ToString()); };
calculatorServiceHost.Open();
}
Console.Read();
}
再运行客户端程序,结果报一下错误:
Message:无法连接到 http://localhost:8889/GeneralCalculator。TCP 错误代码 10061:
由于目标计算机积极拒绝,无法连接。 127.0.0.1:8889。