Search code examples
wcfexceptionfaultexception

FaultException handling in client?


I'm trying to implement exception handling in a WCF service. I want this exception to be thrown to the client so it can be logged and handled.

I don't know if the code I've written is right or wrong. This only gets me "faultexception was unhandled by user code" How should i solve so i can handle my the exception in client?

Code in WCF-Service:

try
{

}
catch (FaultException fex)
{
    throw fex;
}
catch (Exception ex)
{
    throw ex;
} 

Code in client:

try
{

}
catch (FaultException fex)
{
    Logger.AddExceptionToDb(fex);
}
catch (Exception ex)
{
    Logger.AddExceptionToDb(ex);
}

Solution

  • You should apply [FaultContract(YourFaultClass)] attribute to method which will throw fault exceptions.

       [OperationContract]
       [FaultContract(typeof(YourFaultClass))]
       void Foo(int par1);
    

    How to throw it from service:

    throw new FaultException<YourFaultClass>(new YourFaultClass());
    

    for more help have a look at: http://msdn.microsoft.com/en-us/library/cc949036.aspx