How use IInterceptor in Castle.DynamicProxy?
Asked Answered
M

3

23

I wrote an example like this

Simple Calculator class :

public class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

implemented "IInterceptor" that provided by DynamicProxy

 [Serializable]
public abstract class Interceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        ExecuteBefore(invocation);
        invocation.Proceed();
        ExecuteAfter(invocation);

    }
    protected abstract void ExecuteAfter(IInvocation invocation);
    protected abstract void ExecuteBefore(IInvocation invocation);
}

Created an Interceptor class and inherited from "Interceptor" class

    public class CalculatorInterceptor : Interceptor
{
    protected override void ExecuteBefore(Castle.DynamicProxy.IInvocation invocation)
    {
        Console.WriteLine("Start");
    }

    protected override void ExecuteAfter(Castle.DynamicProxy.IInvocation invocation)
    {
        Console.WriteLine("End");
    }
}

but when I used it NOT working !!!

static void Main(string[] args)
    {
        ProxyGenerator generator = new ProxyGenerator();
        Calculator c = generator.CreateClassProxy<Calculator>(new CalculatorInterceptor());
        var r = c.Add(11, 22);
        Console.WriteLine(r);
        Console.ReadKey();
    }

I excepted to see something like this :

START
33
END

but only show

33

How I can correct it ?!

Moe answered 14/2, 2015 at 18:17 Comment(0)
C
24

Try to make the method Add virtual.

public class Calculator
{
    public virtual int Add(int a, int b)
    {
        return a + b;
    }
}

The proxy generator creates a new class inheriting Calculator. Thus, the method Add gets an override to make interception possible.

Cris answered 14/2, 2015 at 19:6 Comment(2)
yes, seems ok but a question the result is START END 33 how I can set Before & After in correct way and see result like this START 33 END , Can I correct this in Interceptor class for correct methods call ??!!!Moe
@Moe the interceptor executes right before and after the Add call, but you write the result after that to the console (execute before -> execute Add -> execute after -> write result). If you make the console call within the Add method, you should get the desired result.Cris
S
14

The other option is to make an ICalculator interface

public interface ICalculator
{
   int Add(int a, int b);
}

and inherit your class from this interface

public class Calculator : ICalculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

Your dynamic proxy would then use the CreateInterfaceProxyWithTarget method

var proxyGenerator = new ProxyGenerator();

ICalculator calculator = new Calculator()

var proxy = proxyGenerator.CreateInterfaceProxyWithTarget(
    calculator,
    ProxyGenerationOptions.Default,
    new CalculatorInterceptor());

Console.WriteLine(proxy.Add(1, 2));

This gets rid of the virtual from your Calculator class, which in my opinion is bad design unless you have reason to override the method in the future.

Sayyid answered 26/10, 2016 at 22:59 Comment(0)
P
0

You have to use the correct overload and pass in both the target object and the interceptor you wish to use. Method should look something like this:

var proxy = generator.CreateClassProxy<Calculator>(new Calculator(), new CalculatorInterceptor() );
Preconcerted answered 14/2, 2015 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.