public bool Open(int timeout) { if (_isDisposed) throw new ObjectDisposedException(this.GetType().Name); if (ChangeStateSafe(StableConnectionState.Opening) != StableConnectionState.Created) throw new InvalidOperationException("Can't open channel which is in state: " + _state.ToString()); try { if (timeout < 0) _channel.Open(); else _channel.Open(TimeSpan.FromMilliseconds(timeout)); if (_channel.State == CommunicationState.Opened) OnOpened(); else Close(); } catch (CommunicationException cex) { Logger.Logger.Instance.Debug(cex, "Error during opening the connection to " + TargetName); OnClosed(); } catch (TimeoutException tex) { Logger.Logger.Instance.Debug(tex, "Error during opening the connection to " + TargetName); OnClosed(); } return State == StableConnectionState.Opened; }
private bool _WCFサービスを取得する(out ChannelFactory <IDTXManiaService> factory, out IDTXManiaService service, out IClientChannel serviceChannel) { const int 最大リトライ回数 = 1; for (int retry = 1; retry <= 最大リトライ回数; retry++) { try { var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None); factory = new ChannelFactory <IDTXManiaService>(binding); service = factory.CreateChannel(new EndpointAddress(endPointUri)); serviceChannel = service as IClientChannel; // サービスとチャンネルは同じインスタンス。 serviceChannel.Open(); return(true); // 取得成功。 } catch { // 取得失敗。少し待ってからリトライする。 if (最大リトライ回数 != retry) { System.Threading.Thread.Sleep(500); } continue; } } serviceChannel = null; service = null; factory = null; return(false); // 取得失敗。 }
/// <summary>显式打开当前的WCF通道,如果通道不存在或异常,则创建新的通道</summary> public static bool Open() { lock (_ans) { IClientChannel channel = _backupsClass as IClientChannel; if (null != _backupsClass && null != channel) { if (channel.State == CommunicationState.Opened || channel.State == CommunicationState.Opening) { try { if (!_channelState) { _channelState = true; } return(true); } catch { } } if (channel.State == CommunicationState.Created || channel.State == CommunicationState.Closed || channel.State == CommunicationState.Closing) { try { channel.Open(); if (!_channelState) { _channelState = true; } return(true); } catch { } } Close(); } return(CreatNewChannel()); } }
public async Task <byte[]> ExecuteWcfProcess(byte[] message) { IService proxy = null; IClientChannel ch = null; byte[] result; try { proxy = _factory.CreateChannel(); ch = (IClientChannel)proxy; ch.Open(); result = await proxy.ExecuteWcfProcess(message); ch.Close(); } catch (Exception) { if (ch != null) { if (ch.State == CommunicationState.Faulted) { ch.Abort(); } else { ch.Close(); } } throw; } return(result); }
public static R Run <T, R>(CachedChannelFactory <T> factory, TimeSpan?timeout, Func <T, R> methodToCall) { T t = factory.Factory.CreateChannel(); R result; using (IClientChannel clientChannel = (IClientChannel)((object)t)) { if (timeout != null) { clientChannel.OperationTimeout = timeout.Value; } CommunicationState state = clientChannel.State; bool flag = false; if (state != CommunicationState.Created) { if (state != CommunicationState.Closed) { goto IL_51; } } try { clientChannel.Open(); flag = true; } catch { clientChannel.Abort(); throw; } IL_51: bool flag2 = false; try { result = methodToCall(t); } catch (Exception error) { if (WcfUtils.IsChannelException(error)) { flag2 = true; clientChannel.Abort(); } throw; } finally { if (!flag2 && flag) { WcfUtils.CloseChannel(clientChannel); } } } return(result); }
IMessage InvokeBegin(IMessage message) { MethodCallMessageWrapper methodCallWrapper = new MethodCallMessageWrapper((IMethodCallMessage)message); FlowContextForTest(methodCallWrapper); IClientChannel channel = m_Factory.CreateChannel() as IClientChannel; channel.OperationTimeout = TimeSpan.MaxValue; channel.Open(); OnPreInvoke(methodCallWrapper.MethodBase as MethodInfo); IMessage response = InjectContinuation(message, channel, methodCallWrapper); return(response); }
void CallNotificationServices(IList <NotificationEvent> notificationEvents, Uri listenerUri) { INotificationService notificationService = null; IClientChannel clientChannel = null; // See the following links which explain the rather sophisticated exception handling below (instead of a simple "using() {})" // * http://msdn.microsoft.com/en-us/library/aa354510.aspx // * http://msdn.microsoft.com/en-us/library/aa355056.aspx try { // For now, we open the channel each and every time. We could do channel caching if we see performance problems // Do not attempt to create the channel if the factory was deleted (shutdown case) if (this.NotificationServiceChannelFactory != null) { notificationService = this.NotificationServiceChannelFactory.CreateChannel(new EndpointAddress(listenerUri)); clientChannel = notificationService as IClientChannel; } if (notificationService != null && clientChannel != null) { clientChannel.Open(); notificationService.Notify(notificationEvents); clientChannel.Close(); } } catch (Exception ex) { //ManagementEtwProvider.Provider.EventWriteThrowingException(ex.ToString()); if (clientChannel != null) { clientChannel.Abort(); } if (!(ex is CommunicationException || ex is TimeoutException || ex is InvalidOperationException)) { throw; } } }
public void Open() { if (_IsDisposed) { throw new ObjectDisposedException("ExportService"); } // Creating channel factory var factory = new DuplexChannelFactory <IExportService>( new InstanceContext(this), new NetNamedPipeBinding()); // Creating server channel _ExportService = factory.CreateChannel(new EndpointAddress(_ServiceAddress)); IClientChannel channel = (IClientChannel)_ExportService; channel.Open(); _ExportService.Subscribe(); }
public void Use(UseServiceDelegate <T> action, string endPoint) { try { _proxy = GetChannelFactory(endPoint).CreateChannel() as IClientChannel; if (_proxy != null) { _proxy.Open(); action((T)_proxy); _proxy.Close(); } } catch (CommunicationException communicationException) { if (_proxy != null) { _proxy.Abort(); } throw communicationException; } catch (TimeoutException timeoutException) { if (_proxy != null) { _proxy.Abort(); } throw timeoutException; } catch (Exception ex) { if (_proxy != null) { _proxy.Abort(); } throw ex; } }
public IPCClient(string serverIP, string hostName) { var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None) { // Set 'infinite' timeouts. SendTimeout = TimeSpan.MaxValue, ReceiveTimeout = TimeSpan.MaxValue }; var factory = new DuplexChannelFactory<IService>(new InstanceContext(this), binding, new EndpointAddress($"net.pipe://{serverIP}/{hostName}")); proxy = factory.CreateChannel(); proxyChannel = proxy as IClientChannel; proxyChannel.Opened += (o, e) => { Servers = proxy.Servers; Redirects = proxy.Redirects; }; proxyChannel.Open(); }
public IPCClient(string serverIP, string hostName) { var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None) { // Set 'infinite' timeouts. SendTimeout = TimeSpan.MaxValue, ReceiveTimeout = TimeSpan.MaxValue }; var factory = new DuplexChannelFactory <IService>(new InstanceContext(this), binding, new EndpointAddress($"net.pipe://{serverIP}/{hostName}")); proxy = factory.CreateChannel(); proxyChannel = proxy as IClientChannel; proxyChannel.Opened += (o, e) => { Servers = proxy.Servers; Redirects = proxy.Redirects; }; proxyChannel.Open(); }
public void UseAsync(UseServiceDelegate <T> action, string endPoint, AsyncCallback callBack, object obj) { try { _proxy = GetChannelFactory(endPoint).CreateChannel() as IClientChannel; if (_proxy != null) { _proxy.Open(); _callBack = callBack; _action = action; IAsyncResult result = _action.BeginInvoke((T)_proxy, AsyncResult, obj); } } catch (CommunicationException communicationException) { if (_proxy != null) { _proxy.Abort(); } throw communicationException; } catch (TimeoutException timeoutException) { if (_proxy != null) { _proxy.Abort(); } throw timeoutException; } catch (Exception ex) { if (_proxy != null) { _proxy.Abort(); } throw ex; } }
/// <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); } } }
public override IMessage Invoke(IMessage message) { MethodCallMessageWrapper methodCallWrapper = new MethodCallMessageWrapper((IMethodCallMessage)message); if (!message.Properties.Contains("retryCount")) { message.Properties.Add("retryCount", 0); } IMessage response = null; IClientChannel channel = null; try { channel = m_Factory.CreateChannel() as IClientChannel; channel.OperationTimeout = TimeSpan.MaxValue; channel.Open(); response = InvokeAsync(channel, methodCallWrapper).Result; Task result = ((response as IMethodReturnMessage).ReturnValue as Task); if (result.IsFaulted) { response = new ReturnMessage(EvaluateException(result.Exception), methodCallWrapper); response = Retry(message, response); } return(response); } catch (TargetInvocationException exception) { if (response == null) { response = new ReturnMessage(exception.InnerException, methodCallWrapper); } return(Retry(message, response)); throw exception.InnerException; } catch (TimeoutException exception) { if (response == null) { response = new ReturnMessage(exception, methodCallWrapper); } return(Retry(message, response)); throw; } finally { if (channel.State != CommunicationState.Closed && channel.State != CommunicationState.Faulted) { try { channel.Close(); } catch { channel.Abort(); } } channel = null; } }
/// <summary> /// Opens the associated client proxy instance. /// </summary> public void Open() { channel.Open(); }
public override IMessage Invoke(IMessage message) { MethodCallMessageWrapper methodCallWrapper = new MethodCallMessageWrapper((IMethodCallMessage)message); if ((message as IMethodMessage).MethodName.Equals("get_Id")) { return(new ReturnMessage(m_ActorId, null, 0, methodCallWrapper.LogicalCallContext, methodCallWrapper)); } if (!message.Properties.Contains("retryCount")) { message.Properties.Add("retryCount", 0); } IMessage response = null; IClientChannel channel = null; try { channel = m_Factory.CreateChannel() as IClientChannel; channel.OperationTimeout = TimeSpan.MaxValue; //Do not add context. Treat each message as an activation request so that we can reassign the InstanceId on the service-side. channel.Open(); response = InvokeAsync(channel, methodCallWrapper).Result; Task result = ((response as IMethodReturnMessage).ReturnValue as Task); if (result.IsFaulted) { response = new ReturnMessage(EvaluateException(result.Exception), methodCallWrapper); response = Retry(message, response); } return(response); } catch (TargetInvocationException exception) { if (response == null) { response = new ReturnMessage(exception.InnerException, methodCallWrapper); } return(Retry(message, response)); throw exception.InnerException; } catch (TimeoutException exception) { if (response == null) { response = new ReturnMessage(exception, methodCallWrapper); } return(Retry(message, response)); throw; } finally { if (channel.State != CommunicationState.Closed && channel.State != CommunicationState.Faulted) { try { channel.Close(); } catch { channel.Abort(); } } channel = null; } }