//http模式
calcFactory = new ChannelFactory<ICalcService>(new BasicHttpBinding(BasicHttpSecurityMode.None), address);
//tcp模式
calcFactory = new ChannelFactory<ICalcService>(new NetTcpBinding(SecurityMode.None), address);
calcService = calcFactory.CreateChannel();
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10; i++)
{
try
{
calcService.Add(3, 4);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
var cObj = calcService as ICommunicationObject;
if (cObj.State == CommunicationState.Faulted
|| cObj.State == CommunicationState.Closed
|| cObj.State == CommunicationState.Closing
)
{
Console.WriteLine("status:closed,closing,faulted");
}
break;
}
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
是如果遇到一次错误,那么会将cObj的状态改变,并且cObj对象不能再次使用
Console.WriteLine("status:closed,closing,faulted");有输出
遇到错误后不会执行到Console.WriteLine("status:closed,closing,faulted");有输出
cObj对象可以重复使用
参考:http://www.cnblogs.com/artech/archive/2009/07/04/1516908.html
原文:http://www.cnblogs.com/wdfrog/p/6323114.html