diff --git a/test/Autofac.Specification.Test/Features/DecoratorTests.cs b/test/Autofac.Specification.Test/Features/DecoratorTests.cs index e3174d235..7e337b234 100644 --- a/test/Autofac.Specification.Test/Features/DecoratorTests.cs +++ b/test/Autofac.Specification.Test/Features/DecoratorTests.cs @@ -281,6 +281,26 @@ public void DecoratorAppliedOnlyOnceToComponentWithExternalRegistrySource() Assert.IsType(service.Decorated); } + [Fact] + public void DecoratorCanBeAppliedTwiceIntentionallyWithExternalRegistrySource() + { + // #965: A nested lifetime scope that has a registration lambda + // causes the decorator to be applied twice - once for the container + // level, and once for the scope level. + var builder = new ContainerBuilder(); + builder.RegisterType().As(); + builder.RegisterDecorator(); + builder.RegisterDecorator(); + var container = builder.Build(); + + var scope = container.BeginLifetimeScope(b => { }); + var service = scope.Resolve(); + + Assert.IsType(service); + Assert.IsType(service.Decorated); + Assert.IsType(service.Decorated.Decorated); + } + [Fact] public void DecoratorCanBeAppliedTwice() { diff --git a/test/Autofac.Test/Features/Decorators/OpenGenericDecoratorTests.cs b/test/Autofac.Test/Features/Decorators/OpenGenericDecoratorTests.cs index 35dbe802f..7b9e2b537 100644 --- a/test/Autofac.Test/Features/Decorators/OpenGenericDecoratorTests.cs +++ b/test/Autofac.Test/Features/Decorators/OpenGenericDecoratorTests.cs @@ -550,6 +550,25 @@ public void DecoratorAppliedOnlyOnceToComponentWithExternalRegistrySource() Assert.IsType>(service.Decorated); } + [Fact] + public void DecoratorCanBeAppliedTwiceIntentionallyWithExternalRegistrySource() + { + // #965: A nested lifetime scope that has a registration lambda + // causes the decorator to be applied twice - once for the container + // level, and once for the scope level. + var builder = new ContainerBuilder(); + builder.RegisterGeneric(typeof(ImplementorA<>)).As(typeof(IDecoratedService<>)); + builder.RegisterGenericDecorator(typeof(DecoratorA<>), typeof(IDecoratedService<>)); + builder.RegisterGenericDecorator(typeof(DecoratorA<>), typeof(IDecoratedService<>)); + var container = builder.Build(); + + var scope = container.BeginLifetimeScope(b => { }); + var service = scope.Resolve>(); + Assert.IsType>(service); + Assert.IsType>(service.Decorated); + Assert.IsType>(service.Decorated.Decorated); + } + [Fact] public void DecoratorCanBeAppliedTwice() {