user824910
user824910

Reputation: 1117

WCF FaultException<T> is not caught in client, instead caught as service fault

I have service configured for FaultException but on the client end I am not getting the exception caught in

catch (FaultException<MyServiceFault> fe)
{
} 

instead it is always caught in

catch (FaultException fx) 
{ 
}

I am using selfhost and channelfactory.

my Service:

[FaultContract(typeof(MyServiceFault))]
public string HelloWorld()
{
    int a=5;
    try
    {
        var b = a/0;
    }
    catch(Exception e)
    {
        throw new FaultException<MyServiceFault>(new MyServiceFault(){Message ="Divide by zero"}, "Divide by Zero");
    }
}

I also have the [DataContract] attribute on the MyServiceFault. I am wondering if I miss any configuration.

Upvotes: 1

Views: 369

Answers (1)

Ricardo Pontual
Ricardo Pontual

Reputation: 3757

I've answered a similar question here: Proper way to throw exception over WCF

Try to declare your operation like this:

[FaultContractAttribute(
        typeof(MyServiceFault),
        Action = "", 
        Name = "MyServiceFault", 
        Namespace = "YourNamespace")]
public string HelloWorld()

Hope it helps.

Upvotes: 1

Related Questions