0

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.

Community
  • 1
  • 1
EvilDr
  • 7,561
  • 10
  • 60
  • 114

1 Answers1

2

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.

Garett
  • 15,936
  • 4
  • 53
  • 62
  • Yes I thought that might be the case. So, by default, the detail string is not passed onto the consuming application? So I suppose that string is for internal logging or something? Thanks. – EvilDr Dec 14 '12 at 16:18
  • 1
    The detail is not necessarily a string. You can have `FaultException(Of ComplexType)` – John Saunders Dec 14 '12 at 16:22