Handling exceptions in WCF service?

shujaat siddiqui picture shujaat siddiqui · Jan 2, 2014 · Viewed 9.3k times · Source

I have project in which i am calling soap based service . I am using fault and communication exception as mostly two of these exception occurs in calling of api. but what if any other exception occurs instead of these two exception in soap api ?

    try
    {
      //soap api
    }
    catch(FaultException ex)
    {
    }
    catch(CommunicationException ex)
    {
    }

Later on i read on internet about soap exception class. and i am considering to change my code like :

     try
    {
      //soap api
    }

    catch(SoapException ex)
    {
    }

Now what i need to know is if i use Soap Exception instead of fault and communication exception . would it be good ? all i need is the catch the exception if it occurs in soap api. however i want to know does the soap api also handles the fault and communication exception or i have to explicitly define my code like ?

 try
    {
      //soap api
    }
    catch(FaultException ex)
    {
    }
    catch(CommunicationException ex)
    {
    }
    catch(SoapException ex)
    {
    }

Answer

John Saunders picture John Saunders · Jan 2, 2014

You will never see a SoapException. That's part of the obsolete ASMX technology.

See Specifying and Handling Faults in Contracts and Services.