0

In WCF , I thought I handle all the cases where the client can become faulted by subscribing to Closed and Faulted event like below

private ConcurrentDictionary<string,ICallback> dict=new ConcurrentDictionary<string,ICallback>();

public void Initialize(IClientChannel channel)
    {

        channel.Closed += new EventHandler(disconnected);
        channel.Faulted += new EventHandler(disconnected);
    }

   private void disconnected(object sender, EventArgs e)
   {
    //Remove from dict
   }

but I am still getting CommunicationObjectAbortedException. I wrap the method in the thread and try-catch block when the service want to raise an event to a client like this.

private void SendEvent(string key)
{

Task.Factory.StartNew(() =>
            {

                    ICallback toRaiseEvent;

                    if (dict.TryGetValue(key, out toRaiseEvent))
                    {
                        try
                        {
                            if (IsChannelValid(toRaiseEvent))
                            {
                                toRaiseEvent.OnEvent();
                            }
                        }
                        catch(TimeoutException e)
                        {

                        }
                        catch
                        {
                        }

                    }

            }
            );
}

   private bool IsChannelValid(IMediaPlayerCallback callback)
    {
        ICommunicationObject comObj = (ICommunicationObject)callback;

        if (comObj.State == CommunicationState.Opened || comObj.State == CommunicationState.Opening)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

The stack trace from the wcf service is as below

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>131075</EventID><Type>3</Type><SubType Name="Error">0</SubType><Level>2</Level><TimeCreated SystemTime="2015-04-23T08:46:52.6535251Z" /><Source Name="System.ServiceModel" /><Correlation ActivityID="{1d3c72e6-a5bb-46bf-b09f-8352e7ed7cfe}" /><Execution ProcessName="MyServiceHosting" ProcessID="2232" ThreadID="4" /><Channel /><Computer>SERVER</Computer></System><ApplicationData><TraceData><DataItem><TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error"><TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ThrowingException.aspx</TraceIdentifier><Description>Throwing an exception.</Description><AppDomain>KaraokeServiceHosting.exe</AppDomain><Source>System.ServiceModel.Channels.ServerReliableDuplexSessionChannel/59817589</Source><Exception><ExceptionType>System.ServiceModel.CommunicationObjectAbortedException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>The communication object, System.ServiceModel.Channels.ServerReliableDuplexSessionChannel, cannot be used for communication because it has been Aborted.</Message><StackTrace>   at System.ServiceModel.Channels.CommunicationObject.ThrowIfAborted()
   at System.ServiceModel.Channels.InputQueueChannel`1.EndDequeue(IAsyncResult result, TDisposable&amp;amp; item)
   at System.ServiceModel.Channels.DuplexChannel.EndTryReceive(IAsyncResult result, Message&amp;amp; message)
   at System.ServiceModel.Dispatcher.DuplexChannelBinder.EndTryReceive(IAsyncResult result, RequestContext&amp;amp; requestContext)
   at System.ServiceModel.Dispatcher.ErrorHandlingReceiver.EndTryReceive(IAsyncResult result, RequestContext&amp;amp; requestContext)
   at System.ServiceModel.Dispatcher.ChannelHandler.EndTryReceive(IAsyncResult result, RequestContext&amp;amp; requestContext)
   at System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result)
   at System.ServiceModel.Dispatcher.ChannelHandler.OnAsyncReceiveComplete(IAsyncResult result)
   at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
   at System.ServiceModel.Diagnostics.TraceUtility.&amp;lt;&amp;gt;c__DisplayClass4.&amp;lt;CallbackGenerator&amp;gt;b__2(AsyncCallback callback, IAsyncResult result)
   at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
   at System.Runtime.InputQueue`1.AsyncQueueReader.Set(Item item)
   at System.Runtime.InputQueue`1.Shutdown(Func`1 pendingExceptionGenerator)
   at System.ServiceModel.Channels.InputQueueChannel`1.OnClosing()
   at System.ServiceModel.Channels.ReliableDuplexSessionChannel.OnClosing()
   at System.ServiceModel.Channels.CommunicationObject.Abort()
   at System.ServiceModel.Dispatcher.DuplexChannelBinder.Abort()
   at System.ServiceModel.Channels.ServiceChannel.OnAbort()
   at System.ServiceModel.Channels.CommunicationObject.Abort()
   at System.ServiceModel.Channels.ServiceChannel.OnInnerChannelFaulted(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.ServiceModel.Channels.CommunicationObject.OnFaulted()
   at System.ServiceModel.Channels.InputQueueChannel`1.OnFaulted()
   at System.ServiceModel.Channels.ReliableDuplexSessionChannel.OnFaulted()
   at System.ServiceModel.Channels.ServerReliableDuplexSessionChannel.OnFaulted()
   at System.ServiceModel.Channels.CommunicationObject.Fault()
   at System.ServiceModel.Channels.CommunicationObject.Fault(Exception exception)
   at System.ServiceModel.Channels.ChannelReliableSession.OnLocalFault(Exception e, Message faultMessage, RequestContext context)
   at System.ServiceModel.Channels.ChannelReliableSession.OnLocalFault(Exception e, WsrmFault fault, RequestContext context)
   at System.ServiceModel.Channels.ChannelReliableSession.OnInactivityElapsed(Object state)
   at System.ServiceModel.Channels.InterruptibleTimer.OnTimerElapsed()
   at System.ServiceModel.Channels.InterruptibleTimer.OnTimerElapsed(Object state)
   at System.Runtime.ActionItem.DefaultActionItem.TraceAndInvoke()
   at System.Runtime.ActionItem.DefaultActionItem.Invoke()
   at System.Runtime.ActionItem.CallbackHelper.InvokeWithoutContext(Object state)
   at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
   at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace><ExceptionString>System.ServiceModel.CommunicationObjectAbortedException: The communication object, System.ServiceModel.Channels.ServerReliableDuplexSessionChannel, cannot be used for communication because it has been Aborted.</ExceptionString></Exception></TraceRecord></DataItem></TraceData></ApplicationData></E2ETraceEvent>

Did I also need to listen for other event to check for faulted state of the client? Does the stack trace even mean that the error is on client side? I have look up handling-dropped-clients and

detecting-client-death

but they only explain how the detect the dead client cases which I have already implemented.

Community
  • 1
  • 1
Sun Maung Oo
  • 183
  • 2
  • 11

1 Answers1

0

Your IsChannelValid method does not adhere to the proper method. You can end up with a race condition by doing that.

From MSDN Checking the value of the ICommunicationObject.State property is a race condition and is not recommended to determine whether to reuse or close a channel.

Stix
  • 485
  • 4
  • 13