EvilDr
EvilDr

Reputation: 9610

How to read FaultException detail in consuming application?

In my WCF WebMethod I have the following code that provides some parameter validation. It works okay and throws the 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

In the consuming application I then call the method in such a way as to trigger the error (to check it works). I catch the error to provide feedback, like this:

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

Within the trace output however, I only see this:

FaultException Caught

First name must be a string value, and not empty.

Can anyone enlighten me please as to how I can read the FaultException detail as well as the reason provided when the FaultException was thrown?

Thanks in advance.

Upvotes: 0

Views: 2150

Answers (1)

Garett
Garett

Reputation: 16838

To get the exception detail you would create a custom fault contract, which is included in the details of the FaultException. Additionally, you would include the FaultReason when throwing an exception. See the following articles for more information.

Upvotes: 2

Related Questions