在WCF中,我以为我通过订阅如下所示的已关闭和故障事件来处理客户端可能出现故障的所有情况
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; item)
at System.ServiceModel.Channels.DuplexChannel.EndTryReceive(IAsyncResult result, Message&amp; message)
at System.ServiceModel.Dispatcher.DuplexChannelBinder.EndTryReceive(IAsyncResult result, RequestContext&amp; requestContext)
at System.ServiceModel.Dispatcher.ErrorHandlingReceiver.EndTryReceive(IAsyncResult result, RequestContext&amp; requestContext)
at System.ServiceModel.Dispatcher.ChannelHandler.EndTryReceive(IAsyncResult result, RequestContext&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.&lt;&gt;c__DisplayClass4.&lt;CallbackGenerator&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和
但他们只解释了如何检测我已经实施的死客户案例。
答案 0 :(得分:0)
您的IsChannelValid方法不符合正确的方法。这样做可以最终达到竞争条件。
From MSDN 检查ICommunicationObject.State属性的值是竞争条件,不建议确定是重用还是关闭通道。