Link to home
Start Free TrialLog in
Avatar of swilson33
swilson33

asked on

catching WCF FaultException

Hi,

I have a WCF service that uses message security with a custom validator.

In my validator I throw a FaultException if the username and/ir password is incorrect.  When I try and catch this is my client it is returned as a MessageSecurityException - the original faultException is in the InnerException.

How do I trap the original FaultException thrown by the validator?

Thanks.
Avatar of Aaron Jabamani
Aaron Jabamani
Flag of United Kingdom of Great Britain and Northern Ireland image

Can we see ur Service Contract Interface class and how ur throwing the fault exception ?
Avatar of swilson33
swilson33

ASKER

Sorry for the delay in replying.

I throw the exception as follows within my validator:

public override void Validate(string userName, string password)
        {
           
                    throw new FaultException("User " + userName + " is already logged in to the service");
           
        }

And here is the contract interface (not in its entirety):

[ServiceContract(Name="Service", Namespace="http://Service.com", SessionMode=SessionMode.Required)]
    [ServiceKnownType(typeof(sVehicleInput))]
    [ServiceKnownType(typeof(dPoint))]
    public interface IDstService
    {
        [OperationContract(IsInitiating = true, IsTerminating = false, IsOneWay = false)]
        void InvokeService();

        [OperationContract(IsInitiating = false, IsTerminating = true, IsOneWay = false)]
        [FaultContract(typeof(FaultExceptionCustomType))]
        void CleanUpAndCloseService();
    }



       
   
Is there any callee method to your validate method which is cathing this exception and throwing MessageSecurityException ?

Are you using ExceptionShielding using Ent Lib?
I'm attempting to catch the faultExeption within my client application but it is caught either as a MessageSecurityException or a CommunicationsException (which I belive is the base class for both FaultExeption and MessageSecurityException).  I'm currently dealing with this by including the InnerException in my exception handling.  I'm not throwing MessageSecurityException from within the client - I'm actually wrapping all exceptions into a custom one and throwing that.

I'm not using ExceptionShielding.  
You should throw the error like below and not the normal way.

public override void Validate(string userName, string password)
        {
           
                  //if your custom exception type gets the detail in constructor use below way. Else use your appropriate way to se the message to the instance.
                    throw new FaultException<FaultExceptionCustomType>( new FaultExceptionCustomType("User " + userName + " is already logged in to the service"));
           
        }
Tried it but still coming back as MessageSecurityException.

Just for info, the validator is a separate assembly to the service.  It's in it's own DLL.  I tell the service to use the validator as follows:

<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Validator.CustomPasswordValidator, Validator"/>

ASKER CERTIFIED SOLUTION
Avatar of swilson33
swilson33

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for sharing the information.
Thanks for your efforts apeter!