【问题标题】:WCF : Strange CommunicationObjectAbortedExceptionWCF:奇怪的 CommunicationObjectAbortedException
【发布时间】:2015-04-23 23:10:47
【问题描述】:

在 WCF 中,我认为我可以通过订阅如下所示的 Closed 和 Faulted 事件来处理客户端可能出现故障的所有情况

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
   }

但我仍然收到 CommunicationObjectAbortedException。当服务想要向这样的客户端引发事件时,我将方法包装在线程和 try-catch 块中。

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;
        }
    }

来自 wcf 服务的堆栈跟踪如下

<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>

我是否还需要监听其他事件来检查客户端的故障状态?堆栈跟踪是否甚至意味着错误在客户端?我查了handling-dropped-clients

detecting-client-death

但他们只解释了如何检测我已经实施的死客户案例。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    您的 IsChannelValid 方法没有遵循正确的方法。这样做可能会导致竞争条件。

    From MSDN 检查 ICommunicationObject.State 属性的值是一种竞争条件,不建议确定是重用还是关闭通道。

    【讨论】:

      猜你喜欢
      • 2013-01-04
      • 2012-07-13
      • 1970-01-01
      • 2011-04-07
      • 2021-02-13
      • 2021-10-01
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多