/// <summary>显式关闭和销毁当前的WCF通道</summary> public static void Close() { lock (_ans) { IClientChannel channel = _backupsClass as IClientChannel; try { if (null == channel) { return; } ClearAllEvents(channel); if (channel.State == CommunicationState.Opened || channel.State == CommunicationState.Opening || channel.State == CommunicationState.Created) { channel.Close(); } } catch { } finally { _backupsClass = null; _channelState = false; } } }
/// <summary>新建一个WCF的通道,返回通道是否有效</summary> private static bool CreatNewChannel() { lock (_ans) { try { WcfClientHelper.SetWCFParas(_ipAdress, _portNum, _binding); _backupsClass = WcfClientHelper.CreateDuplexService <IPluginCtrInterface>("IPluginCtrInterface", _callBack); IClientChannel channel = _backupsClass as IClientChannel; if (channel == null) { return(false); } channel.Faulted += ChannelFaulted; if (channel.State != CommunicationState.Opened) { channel.Open(); } if (!_channelState) { _channelState = true; } return(true); } catch { if (_channelState) { _channelState = false; } return(false); } } }