unitycontainer / interception

Unity.Interception package
Apache License 2.0
22 stars 17 forks source link

Interface interception doesn't work if the interface is implemented by a base type of the implementing type #17

Open ENikS opened 5 years ago

ENikS commented 5 years ago

@vmelamed wrote:

The following code illustrates the problem:

public interface ITestCalls
{
    void Test1();
}

public abstract class BaseTestCalls : ITestCalls
{
    public void Test()
    {
    }
}

[Tag("track")]
public class TrackTestCalls : BaseTestCalls
{
}

[Tag("track1")]
public class TrackTestCalls1 : ITestCalls
{
    public void Test()
    {
    }
}

static void Main(string[] args)
{
    IUnityContainer container = new UnityContainer();

    container.AddNewExtension<Interception>();

    container
        .Configure<Interception>()
        .AddPolicy("track")
        .AddMatchingRule<TagAttributeMatchingRule>(
                            new InjectionConstructor("track", false))
        .AddCallHandler<TrackCallHandler>(
                            new ContainerControlledLifetimeManager())
        ;

    container
        .RegisterType<ITestCalls, TrackTestCalls>(
            "track",
            new InterceptionBehavior<PolicyInjectionBehavior>(),
            new Interceptor<TransparentProxyInterceptor>())

        .RegisterType<ITestCalls, TrackTestCalls1>(
            "track1",
            new InterceptionBehavior<PolicyInjectionBehavior>(),
            new Interceptor<TransparentProxyInterceptor>())
        ;

    var test = container.Resolve<ITestCalls>("track");

    test.Test();

    var test1 = container.Resolve<ITestCalls>("track1");

    test1.Test();
}

test.Test() does not exhibit the decorated behavior and the object test is not supplied with a pipeline. However test1.Test() behaves as expected and test1 has the pipeline.

I could not find this being documented anywhere.