Skip to content

EnableClassInterceptors does not take into account KeyFilter-ed parameters in the constructor #56

@elgatov

Description

@elgatov

Describe the Bug

When using EnableClassInterceptors() and registering a class with WithAttributeFiltering(), parameters decorated with KeyFilter will not have their filter taken into account when trying to intercept the class

Steps to Reproduce

using Autofac;
using Autofac.Extras.DynamicProxy;
using Autofac.Features.AttributeFilters;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Pruebas
{
    [TestClass]
    public class TestClassWithKeyedAttributeConstructor
    {
        enum TestEnum { STRING_ONE }
        public class ClassWithKeyedAttributeConstructor
        {
            private readonly string str;

            public ClassWithKeyedAttributeConstructor([KeyFilter(TestEnum.STRING_ONE)] string str) => this.str = str;

            public string GetStr() => str;
        }
        [TestMethod]
        public void TestClassWithKeyedAttributeConstructorCannotBeIntercepted()
        {
            var builder = new ContainerBuilder();
            builder.Register(c => "Hello").Keyed<string>(TestEnum.STRING_ONE);
            builder.Register(c => "World");
            //builder.RegisterType<ClassWithKeyedAttributeConstructor>().WithAttributeFiltering();
            builder.RegisterType<ClassWithKeyedAttributeConstructor>().WithAttributeFiltering().EnableClassInterceptors();
            var container = builder.Build();

            using var scope = container.BeginLifetimeScope();
            Assert.AreEqual("Hello", scope.Resolve<ClassWithKeyedAttributeConstructor>().GetStr());
        }
    }
}

Expected Behavior

Exception with Stack Trace

  Message: 
Assert.AreEqual failed. Expected:<Hello>. Actual:<World>. 

  Stack Trace: 
TestClassWithKeyedAttributeConstructor.TestClassWithKeyedAttributeConstructorCannotBeIntercepted() line 32

Dependency Versions

Autofac: 8.1.1
Autofac.Extras.DynamicProxy: 7.1.0
MSTest: 3.6.2

Additional Info

If we uncomment line 26 and comment line 27. The test will execute correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions