/// <summary> /// Closes this WCF Service instance. /// </summary> /// <param name="isFaulted">if set to <c>true</c> [is faulted].</param> protected virtual void CleanUp(bool isFaulted) { // Similar to IDisposable we will only execute unsubscribe once, whatever comes first lock (this) { if (Closed) { return; } // Clean up internal connections Closed = true; ServiceManager.Unregister(this); // Close wcf channel _channel.Closed -= OnChannelClosed; _channel.Faulted -= OnChannelFaulted; try { // abort in every case so the client gets a message in every case that the connection is not available anymore _channel.Abort(); } finally { _channel = null; } } }
/// <summary> /// A channel has transitioned to the faulted state /// </summary> /// <param name="sender"></param> /// <param name="e"></param> static void channel_Faulted(object sender, EventArgs e) { try { IContextChannel channel = sender as IContextChannel; if (channel != null) { string typeName = channel.GetType().FullName; if (Channels.ContainsKey(typeName)) { Channels.Remove(typeName); } System.Diagnostics.Debug.WriteLine(string.Format("{0} is in the faulted state.", typeName), string.Format("{0}.{1}.{2}", MethodBase.GetCurrentMethod().DeclaringType.Namespace, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name)); try { channel.Abort(); } catch { // No op... } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message, string.Format("{0}.{1}.{2}", MethodBase.GetCurrentMethod().DeclaringType.Namespace, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name)); } }
private void AbortChannel(object state) { if (_channel != null && _processes < 1) { Log.DebugFormat("Channel for session {0} with client session {1} will be aborted by timeout {2}.", _sessionId, _clientSessionId, _channelTimeout); //TEST: Тестируем RCL if (_clienttype == ClientTypeCode.RCL) { if (_clientSessionId.HasValue) { try { var sessionRegistrator = IoC.Instance.Resolve <ISessionRegistrator>(); sessionRegistrator.EndSession(_clientSessionId.Value, false); //Некорректное закрытие сессии } catch (Exception ex) { Log.Error(string.Format("Error on close ClientSession ('{0}')", _clientSessionId), ex); } _clientSessionId = null; _isClientSessionIdNull = true; } else { if (!_isClientSessionIdNull) { Log.Error(new DeveloperException("_clientSessionId is undefined.")); } } } _channel.Abort(); } }
/// <summary> /// Valida se canal de callback eh valido /// </summary> /// <param name="callback">instancia do callback, retornada em GetCallback[T}()</param> /// <returns>true, se o ChannelState = Opened </returns> public static bool IsValidChannel(object callback) { bool bRet = false; if (callback != null) { lock (_clientChannels) { if (_clientChannels.ContainsKey(callback)) { IContextChannel channel = (IContextChannel)_clientChannels[callback]; if (channel.State == CommunicationState.Opened) { bRet = true; } else { channel.Abort(); _clientChannels.Remove(callback); return(false); } } } } return(bRet); }
/// <summary> /// Close the specified service channel. /// </summary> /// <param name="channel">The channel to close.</param> /// <param name="abort">Whether the close should be an abort.</param> private static void CloseChannel(IContextChannel channel, bool abort) { if (channel != null) { if (abort) { channel.Abort(); } else { channel.Close(); } } }
public void CloseChannel() { if (_channelCache != null) { if (_channelCache.State == CommunicationState.Opened) { _channelCache.Close(); } else if (_channelCache.State == CommunicationState.Faulted) { _channelCache.Abort(); } _channelCache.Closed -= Channel_Closed; _channelCache.Opened -= Channel_Opened; _channelCache.Faulted -= Channel_Faulted; } }
/// <summary> /// Closes the channel /// </summary> private void Close() { try { System.Diagnostics.Trace.WriteLine("Closing channel '" + typeof(T).Name + "'"); // Access m_Channel directly because accessing Channel can cause stack overflow if (m_Channel != null) { // Kill existing channel m_Channel.Faulted -= new EventHandler(Channel_Faulted); try { m_Channel.Close(TimeSpan.FromMilliseconds(100)); } catch (Exception ex) { System.Reflection.MethodBase mb = System.Reflection.MethodBase.GetCurrentMethod(); System.Diagnostics.Trace.WriteLine(ex.Message, string.Format("{0}.{1}.{2}", mb.DeclaringType.Namespace, mb.DeclaringType.Name, mb.Name)); try { m_Channel.Abort(); } catch { // No-op } } } Channel = null; } catch (Exception ex) { System.Reflection.MethodBase mb = System.Reflection.MethodBase.GetCurrentMethod(); System.Diagnostics.Trace.WriteLine(ex.Message, string.Format("{0}.{1}.{2}", mb.DeclaringType.Namespace, mb.DeclaringType.Name, mb.Name)); } }
public override void Dispatch(MessageDelivery messageDelivery) { if (!Started) { throw new InvalidOperationException("Dispatcher is not started yet"); } ChannelFactory factory = CreateFactory(); factory.Endpoint.Address = new EndpointAddress(Endpoint.Address); ApplySecurityContext(messageDelivery, factory); IContextChannel proxy = CreateProxy(factory); using (OperationContextScope scope = new OperationContextScope(proxy)) { bool success = false; try { var lookup = initActionLookup(Endpoint); MethodInfo methodInfo = lookup.MethodLookup[_passThrough ? "*" : messageDelivery.Action]; if (methodInfo != null) { try { object result = methodInfo.Invoke(proxy, new object[] { messageDelivery.Message }); if (lookup.ReplyActionLookup.ContainsKey(messageDelivery.Action)) // if two way message, publish reply { KeyValuePair <MessageDeliveryContextKey, object>[] replyData = new KeyValuePair <MessageDeliveryContextKey, object> [1]; replyData[0] = new KeyValuePair <MessageDeliveryContextKey, object>(MessageDelivery.CorrelationId, GetResponseCorrelationId(messageDelivery)); Runtime.PublishOneWay(new PublishRequest(Endpoint.ContractType, lookup.ReplyActionLookup[messageDelivery.Action], result, new MessageDeliveryContext(replyData))); } } catch (System.Reflection.TargetInvocationException ex) { if (lookup.ReplyActionLookup.ContainsKey(messageDelivery.Action)) // if two way message, publish reply { KeyValuePair <MessageDeliveryContextKey, object>[] replyData = new KeyValuePair <MessageDeliveryContextKey, object> [1]; replyData[0] = new KeyValuePair <MessageDeliveryContextKey, object>(MessageDelivery.CorrelationId, GetResponseCorrelationId(messageDelivery)); FaultException fex = ex.InnerException as FaultException; if (fex != null) { Runtime.PublishOneWay(new PublishRequest(Endpoint.ContractType, fex.Action, ((FaultException)ex.InnerException), new MessageDeliveryContext(replyData))); } else { throw; } } else { throw; } } } else { throw new InvalidOperationException("Matching method not found"); } proxy.Close(); success = true; } finally { if (!success) { proxy.Abort(); } } } }