1

I have a WCF service with wsHttpBinding. Everything works just fine, but I have got problem with catching faults on my client, sent from my custom Authenticator.
I use custom Authenticator code from msdn:
https://msdn.microsoft.com/en-us/library/aa702565(v=vs.110).aspx

// This throws an informative fault to the client.
    throw new FaultException("Unknown Username or Incorrect Password");

this comment says that we are throwing an informative fault to the client, but i can't catch it on the client side:

bool isReachable = false;
                try {
                    isReachable = client.agentIsReachable();
                }
                catch(FaultException faultException){
                    MessageBox.Show(faultException.Message);
                }

While debugging I can see, that a fault is thrown, but my clients catch code does not work. My communication channel faults, but without fault exception. Then i catch a .NET ex, saying that I am trying to use faulted proxy.
Everything works great when i throw faults from any of my service methods. I can catch them on my client.

Is it really possible to catch faults, sent from Authenticator. And what is the best way to pass an informative message to the client when authentication fails?

Tar
  • 11
  • 3

1 Answers1

1

Client-side, the exception you have to catch is not a FaultException but a MessageSecurityException (using System.ServiceModel.Security).

Then you can retrieve your FaultException with the InnerException attribute of the MessagSecurityException you caught. In your case, you'll end up with something similar to this:

catch (MessageSecurityException e)
{
    FaultException fault = (FaultException) e.InnerException;
    MessageBox.Show(faultException.Message);
}

I hope it will help.

tooomg
  • 469
  • 2
  • 7
  • to some extent, this approach is ok but it does not work when FaultException is thrown from server side then it does not catch with this type in SecurityException. – Asad Naeem Dec 26 '17 at 10:21
  • Try this may be it would help you: https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx?f=255&MSPPError=-2147217396 – Asad Naeem Dec 26 '17 at 11:54