Search code examples
c#wcfexceptionwcf-faults

WCF fault handling


Q How can I get the original exception (occurred on the server) on the client side?

I am working with a self-hosted WCF service and C# 4 and trying to set up proper exception handling.

I have a client that looks like this

private ServiceResponse PerformRemoteAction(ServiceRequest request, ProcessOperations operation)
{
    ...    
    try
    {
        //remote call
        switch (operation)
        {
            ...
            case ProcessOperations.VerifyAction: { response = client.VerifyAction(request); break; } 

            default: throw new NotImplementedException();
        }
    }
    catch (Exception ex)
    {
        AppManager.LogException(ex);
    }

    return response;
}

And service implementation

public override ServiceResponse VerifyAction(ServiceRequest request)
{
    RegisterRequest(request);

    try
    {
        var chain = new VerificationChain();
        Responce = chain.Run(request);
    }
    catch (Exception ex)
    {
        throw new FaultException<WcfException>(ExceptionFactory.Create(ex),
            new FaultReason(ex.Message));
    }

    SetSuccessfulResponce();

    return Responce;
}

Service web.config has

<behaviors>
    <serviceBehaviors>
        <behavior>
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
    </serviceBehaviors>
</behaviors>

This is my original exception enter image description here

And this is what I get on the cliententer image description here

If needed I can post full details on the client side, however the exception on the client does not have any reference to the original one.

Update
This is my interface with defined FaultContractAttribute

[ServiceContract]
public interface IServiceOperation : IOperation
{
    ...
    [OperationContract(Action = "http://tempuri.org/ITestRunnerService/VerifyAction", ReplyAction = "http://tempuri.org/ITestRunnerService/VerifyAction")]
    [FaultContractAttribute(typeof(WcfException), Action = "http://tempuri.org/ITestRunnerService/ExecuteRequestWcfExceptionFault", Name = "WcfException", Namespace = "http://schemas.datacontract.org/2004/07/TH.Core.Exceptions")]
    ServiceResponse VerifyAction(ServiceRequest request);
}

And this is my WcfException class

[DataContract]
public class WcfException : Exception
{
    [DataMember]
    public string Title { get; set; }

    [DataMember]
    public new string Message { get; set; }

    [DataMember]
    public new string InnerException { get; set; }

    [DataMember]
    public new string StackTrace { get; set; }
}

Solution

  • Thanks for all your answers. My problem was in the service interface, namely in the Action property of FaultContractAttribute, it pointed to the incorrect URL.

    [ServiceContract]
    public interface IServiceOperation : IOperation
    {
        ...
        [OperationContract(Action = "http://tempuri.org/IServiceOperation/VerifyAction", ReplyAction = "http://tempuri.org/IServiceOperation/VerifyAction")]
        [FaultContractAttribute(typeof(WcfException), Action = "http://tempuri.org/IServiceOperation/ExecuteRequestWcfExceptionFault", Name = "WcfException", Namespace = "http://schemas.datacontract.org/2004/07/TH.Core.Exceptions")]
        ServiceResponse VerifyAction(ServiceRequest request);
    }
    

    I solved it by removing all other properties (to be determined automatically) and left only the type of WcfFault

    [ServiceContract]
        public interface IServiceOperation : IOperation
        {
            ...
            [OperationContract(Action = "http://tempuri.org/IServiceOperation/VerifyAction", ReplyAction = "http://tempuri.org/IServiceOperation/VerifyAction")]
            [FaultContractAttribute(typeof(WcfException))]
            ServiceResponse VerifyAction(ServiceRequest request);
        }