Esteban Martinez
Esteban Martinez

Reputation: 84

WCF Service side Excepion catching FaultException

I am in the middle of learning Fault Exception with WCF and am having a little issue. I would like to see if anyone could suggest the appropriate method for doing the following.

The situation, in my example, is that I have a Login service method. When an invalid login attempt is made, I throw a custom fault exception. I also have a try catch in the method so that any other unknown error is caught and sent to the client as a custom Unknown fault exception.

The issue is that when I throw the AuthenticationFault exception, the general exception catch is also grabbing that and forcefully send the UnknownFault instead.

After reviewing the code, I can understand -why- this is happening. So, it leaves me to ask the community how the appropriate way of handling this should be?

Should I not be using a general exception catch in the service and always allow the client to handle this? I didn't really want to use that approach because then how would I handle other possible unknown exceptions on the server and possibly log them?

Is it possible to have a catch state something to the like of "catch any exception -except- faults"?

Thank, code below.

try
{
    Authenticator AuthTool = new Authenticator();
    if (AuthTool.Authenticate(credentials))
    {
        //--Successful login code
    }
    else
    {
        AuthenticationFault fault = new AuthenticationFault();
        fault.Message = "Invalid Login or Password";
        throw new FaultException<AuthenticationFault>(fault, new FaultReason(fault.Message));
    }
}
catch (Exception ex)
{
    UnknownFault fault = CommonUtil.CreateCommonFault(ex);
    throw new FaultException<UnknownFault>(fault, new FaultReason(fault.ErrorMessage));
}

The "catch (Exception ex)" code above catches the fault exception thrown earlier.

try
{
    //--proxy call to the service Login method
}
catch (FaultException<AuthenticationFault> af)
{
    //--Never gets here
}
catch (FaultException<UnknownFault> uf)
{
    //--This is what is handling although I threw the AuthenticationFault
}
catch (Exception ex)
{
    //--any other unknown error
}

The above is the client-side error handling

Upvotes: 1

Views: 2306

Answers (1)

Mike Parkhill
Mike Parkhill

Reputation: 5551

You need to explicitly catch and rethrow your AuthenticationFault in the first block. Your general catch is converting it to a FaultException<UnknownFault>.

try
{
    Authenticator AuthTool = new Authenticator();
    if (AuthTool.Authenticate(credentials))
    {
        //--Successful login code
    }
    else
    {
        AuthenticationFault fault = new AuthenticationFault();
        fault.Message = "Invalid Login or Password";
        throw new FaultException<AuthenticationFault>(fault, new FaultReason(fault.Message));
    }
}
catch (FaultException<AuthenticationFault>
{
    throw;
}
catch (Exception ex)
{
    UnknownFault fault = CommonUtil.CreateCommonFault(ex);
    throw new FaultException<UnknownFault>(fault, new FaultReason(fault.ErrorMessage));
}

Upvotes: 1

Related Questions