1

A good suggestion on how to handle errors within a Client can be found here.
Copying here for easy access:

MyServiceClient myServiceClient = new MyServiceClient();

try
{
    documents = myServiceClient.GetDocuments();
    // More code that isn't useful including here ...
    myServiceClient.Close();
}
catch (TimeoutException exception)
{
    MessageBox.Show(exception.Message, "Timeout error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    myServiceClient.Abort();
}
catch (FaultException<ServiceErrorDetails> error)
{
    MessageBox.Show(error.Detail.Message, "Service error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    myServiceClient.Abort();
}
catch (CommunicationException exception)
{
    MessageBox.Show(exception.Message, "Communication error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    myServiceClient.Abort();
}

Now the problem I am having with this solution is that my Proxy contains many many methods. Easy to understand I would rather not want to add this huge try/catch statement around all my method calls.
Instead, I thought it could be a good idea to add the error handling from within MyServiceClient() class.
But the question is how to do that without polluting all the Methods here again with this try/catch statement?
How would you approach that?

1 Answer 1

2

You could try encapsulating the try/catch logic in a handler class as follows:

public static class Example
{
    public static void ExecuteExample()
    {
        var serviceClient = new ServiceClient();

        var documents = ProxyErrorHandler.Execute(serviceClient, serviceClient.GetDocuments);
    }
}

public static class ProxyErrorHandler
{
    public static void Execute(ServiceClient serviceClient, Action actionToExecute)
    {
        Execute(serviceClient, () =>
        {
            actionToExecute();

            return true;
        });
    }

    public static T Execute<T>(ServiceClient serviceClient, Func<T> functionToExecute)
    {
        try
        {
            return functionToExecute();
        }
        catch (Exception exception)
        {
            ShowException(serviceClient, exception);

            return default;
        }
    }

    public static Task ExecuteAsync(ServiceClient serviceClient, Func<Task> actionToExecute)
    {
        return ExecuteAsync(serviceClient, async () =>
        {
            await actionToExecute();

            return true;
        });
    }

    public static async Task<T> ExecuteAsync<T>(ServiceClient serviceClient, Func<Task<T>> functionToExecute)
    {
        try
        {
            return await functionToExecute();
        }
        catch (Exception exception)
        {
            ShowException(serviceClient, exception);

            return default;
        }
    }

    private static void ShowException(ServiceClient serviceClient, Exception exception)
    {
        string title;
        var message = exception.Message;

        switch (exception)
        {
            case TimeoutException:
                title = @"Timeout error";
                break;
            case FaultException<ServiceErrorDetails> faultException:
                title = @"Service error";
                message = faultException.Detail.Message;
                break;
            case CommunicationException:
                title = @"Communication error";
                break;
            default:
                ExceptionDispatchInfo.Throw(exception);
                
                // Unreachable
                throw new Exception();
        }

        MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);

        serviceClient.Abort();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Splendid, thanks! I don't have enough reputation to upvote you though :-)
@stackMeUp Haha, not a problem. Can you mark it as the accepted answer? I'm not sure if you can. I'm just glad to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.