Dot-Net

更新到 .net 4.0 後的 wcf 回調異常

  • June 2, 2010

我有一個使用 DualHttpBindings 回調的 wcf 服務。該服務在找到它們時將搜尋結果的數據表推回客戶端(用於長時間執行的搜尋)。

這在 .Net 3.5 中執行良好。自從我更新到 .Net 4.0 後,它會因 System.Runtime.FatalException 而爆炸,實際上會殺死 IIS 工作程序。我什至不知道如何著手解決這個問題。任何建議表示讚賞。

生成的事件日誌中的資訊粘貼在下面:


發生未處理的異常,程序終止。

應用程序 ID:/LM/W3SVC/2/ROOT/CP

程序 ID:5284

> 異常:System.Runtime.FatalException

> 消息:對象引用未設置為對象的實例。

堆棧跟踪:在 System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet) 在 System.ServiceModel.Dispatcher 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)。 ChannelHandler.DispatchAndReleasePump(RequestContext request, Boolean cleanThread, OperationContext currentOperationContext) at System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext request, OperationContext currentOperationContext) at System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result) at System.Runtime.Fx .AsyncThunk.UnhandledExceptionFrame(IAsyncResult 結果) 在 System.Runtime.AsyncResult。在 System.Runtime.InputQueue 完成(布爾完成同步)1.AsyncQueueReader.Set(Item item) at System.Runtime.InputQueue1.Dispatch() 在 System.ServiceModel.Channels.ReliableDuplexSessionChannel.ProcessDuplexMessage(WsrmMessageInfo info) 在 System.ServiceModel.Channels.ReliableDuplexSessionChannel.HandleReceiveComplete(IAsyncResult 結果) 在 System.ServiceModel.Channels.ReliableDuplexSessionChannel.OnReceiveCompletedStatic(IAsyncResult 結果) 在系統。 Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result) at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously) at System.ServiceModel.Channels.ReliableChannelBinder 1.InputAsyncResult1.OnInputComplete(IAsyncResult result) at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult結果)在 System.Runtime.AsyncResult.Complete(Boolean completedSynchronously) 在 System.Runtime.InputQueue1.AsyncQueueReader.Set(Item item) at System.Runtime.InputQueue1.Dispatch() 在 System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)

在 System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 錯誤, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)

在系統。 Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

> 內部異常: > System.NullReferenceException

消息:對象引用未設置為對象的實例。

StackTrace:在 System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext) 在 System.Web.HttpApplication.ThreadContext.Enter(Boolean setImpersonationContext) 在 System.Web.AspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback 回調,對象狀態) 在 System.Web.AspNetSynchronizationContext。**System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)**處的 CallCallback(SendOrPostCallback 回調,對象狀態 )

好的 - 我找到了答案。很奇怪,只是簡單地將以下屬性放在 WCF 回調包裝類上:

[CallbackBehavior(UseSynchronizationContext=false)]

感謝 Cauldwell.net 的回答: http: //www.cauldwell.net/patrick/blog/CategoryView,category,CodeGen.aspx

來自 cauldwell.net:

事實證明,問題在於 ASP.NET(預設情況下)使用了一個叫做 SynchronizationContext 的小東西。據我所知(說實話,我還沒有徹底研究過)其中一項工作是確保在 UI 執行緒上執行任何回調,從而避免像你一樣呼叫 Control.Invoke贏表格。在我的情況下,那個額外的鎖正在給出一些合適的東西,它試圖清理一個不再存在的執行緒上的東西,因此是 NullReferenceException。

引用自:https://stackoverflow.com/questions/2959704