0

I am using WCF and need to catch all the exceptions that can occur when calling a method on the service proxy, so that I can then instead return a status value. I want to do this to encapsulate/hide the underlying WCF implemenation. An example code would be this:

public Status StartProcess(string outputFolder)
{
        return service.StartProcess(outputFolder);
}

service.StartProcess returns a Status-enum and I would like to catch all possible WCF exceptions that can occur from the call to service.StartProcess(outputFolder); so that I can return a corresponding Status-enum. From testing I arrived at this:

public Status StartProcess(string outputFolder)
{
    try
    {
        return service.StartProcess(outputFolder);
    }
    catch (System.ObjectDisposedException) // occurs, when client closed the connection previously
    {
        return Status.ClientDisconnected;
    }
    catch (System.ServiceModel.CommunicationObjectFaultedException) // occurs, when server unexpectedly closed the connection previously
    {
        return Status.CommunicationFailed;
    }
}

So my question is: Are these all the exception I have to handle in this case or are there other exceptions I should anticipate?

packoman
  • 1,230
  • 1
  • 16
  • 36
  • 2
    It's difficult to answer your question, there's a lot of exceptions that can be thrown, I could mention `EndpointNotFoundException`, `TimeoutException`, `MessageSecurityException`, `TargetInvocationException`, `CommunicationException` and `InvalidOperationException`, but could be others. – Ricardo Pontual Nov 01 '16 at 10:30
  • This answer should help: http://stackoverflow.com/a/9370880/569662 – tom redfern Nov 01 '16 at 11:46
  • 2
    Possible duplicate of [What is the best workaround for the WCF client \`using\` block issue?](http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-block-issue). On the surface it's not about the same thing, but many of the answers document many exceptions. – tom redfern Nov 01 '16 at 11:46
  • Also http://stackoverflow.com/questions/5374421/wcf-web-service-call-which-exceptions-to-catch?rq=1 – tom redfern Nov 01 '16 at 11:48

0 Answers0