什麼是 System.ServiceModel.Diagnostics.CallbackException,為什麼我不能處理它?
在我的 WCF 客戶端類中,我正在處理該
Faulted()事件,以便如果遠端服務引發異常並導致通道出現故障,我至少仍然可以正常關閉它。這是我的程式碼:protected void RemoteDataRetriever_Faulted(object sender, EventArgs e) { (sender as ICommunicationObject).Abort(); this.Dispose(); throw new ChannelTerminatedException("The remote service threw an unhandled exception and as a result the channel has been closed."); }所以我期望客戶端可以處理
ChannelTerminatedException我手動拋出的內容並向使用者發送消息等。相反,我的異常被包裹在System.ServiceModel.Diagnostics.CallbackException. 好的。除了這裡有一個問題:這個 CallbackException 在 ServiceModel 庫中不存在,我似乎沒有辦法處理它,除非作為一個 genericException,這對我的單元測試沒有好處。這到底是怎麼回事?我可以以某種方式禁用它並拋出我最初想要的異常嗎?
事實證明,
System.ServiceModel.Diagnostics.CallbackException是一個內部類,填充在一個名為“%SystemRoot%\Microsoft.net\Framework\v3.0\Windows Communication Foundation\SMDiagnostics.dll”的鮮為人知的程序集中,它本身只包含內部類。好吧,這很糟糕,因為這意味著我們永遠無法擷取該異常。但是,我能夠找到實例化上述異常的類/方法 (System.ServiceModel.Diagnostics.ExceptionUtility.ThrowHelperCallback(Exception innerException)) 並發現它是由 CommunicationObject 內的虛擬方法 OnFaulted() 呼叫的。所以理論上任何派生自 CommunicationObject 的類(對不起ClientBase<T>)都可以覆蓋該方法並告訴它不要呼叫 ThrowHelperCallback()。這意味著唯一可行的候選者是派生自ChannelFactoryBase<T>. 從理論上講,我可以繼續實現我自己的自定義通道工廠,它可以抑制煩人的 CallbackException 但目前工作量太大,所以我想我只需要處理它。編輯:@Jeremy - 如果我檢查通過線路返回的 SOAP 信封,我發現它給了我一個一般錯誤,正如預期的那樣,這表明 CallbackException 沒有被序列化,因此沒有在伺服器上生成。
<s:Body> <s:Fault> <s:Code> <s:Value>s:Receiver</s:Value> <s:Subcode> <s:Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</s:Value> </s:Subcode> </s:Code> <s:Reason> <s:Text xml:lang="en-US">The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.</s:Text> </s:Reason> </s:Fault> </s:Body>