2012-12-14 41 views
0

在我的WCF WebMethod中,我有以下代碼提供了一些參數驗證。它的工作原理很好,而且拋出的FaultException:如何閱讀消費應用程序中的FaultException細節?

Public Function BeforeCall(operationName As String, inputs() As Object) As Object Implements IParameterInspector.BeforeCall 
    Dim u As MyAppUser = CType(inputs(0), MyAppUser) 
    If u.FirstName Is Nothing Then 
     Throw New FaultException(Of String)("First Name is Nothing", "First name must be a string value, and not empty.") 
    End If 
    Return u 
End Function 

在消費應用那麼我所說的方法,以觸發錯誤的方式(以檢查它的工作原理)。我趕上了錯誤提供反饋,這樣的:

Dim u As New ServiceReference1.MyAppUser 
u.FirstName = Nothing 
u.Surname = "SomeSurname" 
Dim i As New ServiceReference1.Service1Client 
Dim u2 As New ServiceReference1.MyAppUser 
Try 
    u2 = i.GetDataUsingDataContract(u) 
Catch fe As FaultException 
    Trace.Warn("FaultException Caught") 
    Trace.Warn(fe.Message) 
    Exit Sub 
Catch ex As Exception 
    Throw 
End Try 

在跟蹤輸出然而,我只看到這一點:

的FaultException陷入

的名字必須是一個字符串值,而不是空的。

任何人都可以啓發我,請問我如何閱讀FaultException細節以及FaultException引發時提供的原因?

在此先感謝。

回答

2

要獲得例外詳細信息,您可以創建一個自定義fault contract,其中包含FaultException的詳細信息。此外,您在拋出異常時將包含FaultReason。有關更多信息,請參閱以下文章。

+0

是的,我認爲可能是這種情況。那麼,默認情況下,詳細信息字符串不會傳遞到消費應用程序?所以我想這個字符串是用於內部日誌記錄或者什麼的?謝謝。 – EvilDr

+1

細節不一定是字符串。你可以有'FaultException(Of ComplexType)' –

相關問題