Wrong interceptor is chosen when multiple bindings exist

121 views Asked by At

I have multiple implementations of an interface and I want to apply different interceptors for each of them e.g.:

public interface IFoo { int Run(); }

public class Foo1 : IFoo
{
    public int Run() { return 1; }
}

public class Foo2 : IFoo
{
    public int Run() { return 2; }
}

Example interceptors:

public class MultiplyBy10 : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        invocation.Proceed();
        invocation.ReturnValue = (int)invocation.ReturnValue * 10;
    }
}

public class MultiplyBy100: IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        invocation.Proceed();
        invocation.ReturnValue = (int)invocation.ReturnValue * 100;
    }
}

Binding:

var kernel = new StandardKernel();
kernel.Bind<IFoo>().To<Foo1>().Named("1");
kernel.Bind<IFoo>().To<Foo2>().Named("2");

kernel.Intercept(ctx => ctx.Plan.Type == typeof(Foo1)).With<MultiplyBy10>();
kernel.Intercept(ctx => ctx.Plan.Type == typeof(Foo2)).With<MultiplyBy100>();

I expect Foo1 to be multiplied by 10 and Foo2 by 100. But instead both are multiplied by 10 i.e.:

var foo1 = kernel.Get<IFoo>(ctx => ctx.Name == "1");
var foo2 = kernel.Get<IFoo>(ctx => ctx.Name == "2");

Assert.That(foo1.Run(), Is.EqualTo(10));
// fails: returns 20
Assert.That(foo2.Run(), Is.EqualTo(200));

Is this a bug or am I doing something wrong?

1

There are 1 answers

0
orientman On