1

Consider the below, which works:

public interface IService
{
    void DoSomething(object arg);
    void DoSomethingElse(object arg, bool anotherArg);
    bool AndDoYetMoreStuff(object arg, object[] moreArgs);
}

public class Service : IService
{
    public void DoSomething(object arg){}

    public void DoSomethingElse(object arg, bool anotherArg){}

    public bool AndDoYetMoreStuff(object arg, object[] moreArgs)
    {
        return true;
    }
}

public class ServiceRetryProxy : IService
{
    const int retryLimit = 3;
    private readonly IService _service;

    public ServiceRetryProxy(IService service)
    {
        _service = service;
    }

    private void RetryOnException(Action<IService> ctx)
    {
        ReconnectOnException(service =>
        {
            ctx(service);
            return new object();
        });
    }

    private T RetryOnException<T>(Func<IService, T> ctx)
    {
        var counter = 0;
        Exception lastException = null;
        while (counter < retryLimit)
        {
            try
            {
                return ctx(_service);
            }
            catch (Exception ex)
            {
                lastException = ex;
                counter++;
            }
        }

        throw lastException;
    }

    public void DoSomething(object arg)
    {
        ReconnectOnException(x => x.DoSomething(arg));
    }

    public void DoSomethingElse(object arg, bool anotherArg)
    {
        ReconnectOnException(x => x.DoSomethingElse(arg, anotherArg));
    }

    public bool AndDoYetMoreStuff(object arg, object[] moreArgs)
    {
        return ReconnectOnException(x => x.AndDoYetMoreStuff(arg, moreArgs));
    }
}

The problem with this is that I have to write a proxy method for every method of the interface. I would like a more 'dynamic' solution so that I can apply the RetryOnException (or any other logic) to every method on any given interface. I'm currently looking at Castle DynamicProxy, but what if any are the other options?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116

1 Answers1

0

Castle dynamic proxy is certainly one option.

But I would personally go for postsharp: http://www.sharpcrafters.com/

I already used it for similar scenario. I created RetryOnexceptionAspect where you could specify type of exception, number of retries and apply it on any method:

[Serializable]
public class RetryOnExceptionAspect : MethodInterceptionAspect
{
    private readonly int maxRetries;

    private readonly Type exceptionType;

    public RetryOnExceptionAspect(int maxRetries, Type exceptionType)
    {
        this.maxRetries = maxRetries;
        this.exceptionType = exceptionType;
    }

    public override void OnInvoke(MethodInterceptionArgs args)
    {
        for (int i = 0; i < maxRetries + 1; i++)
        {
            try
            {
                args.Proceed();
            }
            catch (Exception e)
            {
                if (e.GetType() == exceptionType && i < maxRetries)
                {
                    continue;
                }

                throw;
            }

            break;
        }
    }
}

Usage like:

public class Service : IService
{
    [RetryOnExceptionAspect(5, typeof(TimeoutException)]
    public void DoSomething()
    {
    }
}
Peter Zajic
  • 899
  • 8
  • 17