终结者中的WCF NullReferenceException

我们有WCF的问题,我们花了很长时间来诊断,所以我在这里发布他们的情况下,有人有类似的问题(没有find一个参考其他地方)。

我们的进程经常会因下列未处理的exception而崩溃:

System.NullReferenceException: Object reference not set to an instance of an object. at System.ServiceModel.Channels.OverlappedContext.CompleteCallback(UInt32 error, 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) System.NullReferenceException: Object reference not set to an instance of an object. at System.Threading.Overlapped.Free(NativeOverlapped* nativeOverlappedPtr) at System.ServiceModel.Channels.OverlappedContext.Free() at System.ServiceModel.Channels.OverlappedContext.FreeOrDefer() at System.ServiceModel.Channels.SocketConnection.Abort(TraceEventType traceEventType, String timeoutErrorString, TransferOperation transferOperation) at System.ServiceModel.Channels.CommunicationPool`2.EndpointConnectionPool.CloseIdleConnection(TItem connection, TimeSpan timeout) at System.ServiceModel.Channels.IdlingCommunicationPool`2.IdleTimeoutEndpointConnectionPool.IdleTimeoutIdleConnectionPool.OnIdle() 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) System.NullReferenceException: Object reference not set to an instance of an object. at System.Threading.Overlapped.Free(NativeOverlapped* nativeOverlappedPtr) at System.Net.AsyncRequestContext.Dispose(Boolean disposing) at System.Net.RequestContextBase.Finalize() 

例外情况通常发生在几个小时后,有时在几分钟后。

奇怪的是,他们只会在安装2013年1月的Windows更新(并卸载它们后离开)后才会显示。

问题原来是IClientChannel

我们忘记了使用后的通道,并从终结器线程调用Dispose()

我的猜测是,IClientChannel使用非托管资源,必须从创建它们的相同线程释放。

从同一个线程调用Dispose()后,问题就消失了。