diff --git a/src/Scrutor/DecorationStrategy.cs b/src/Scrutor/DecorationStrategy.cs index b392ff85..08789b29 100644 --- a/src/Scrutor/DecorationStrategy.cs +++ b/src/Scrutor/DecorationStrategy.cs @@ -30,11 +30,10 @@ internal static DecorationStrategy WithFactory(Type serviceType, string? service protected static Func TypeDecorator(Type serviceType, string serviceKey, Type decoratorType) { - var factory = ActivatorUtilities.CreateFactory(decoratorType, new[] { serviceType }); return (serviceProvider, _) => { var instanceToDecorate = serviceProvider.GetRequiredKeyedService(serviceType, serviceKey); - return factory(serviceProvider, new object[] { instanceToDecorate }); + return ActivatorUtilities.CreateInstance(serviceProvider, decoratorType, instanceToDecorate); }; } diff --git a/test/Scrutor.Tests/DecorationTests.cs b/test/Scrutor.Tests/DecorationTests.cs index 00fa295a..2d4fc6c4 100644 --- a/test/Scrutor.Tests/DecorationTests.cs +++ b/test/Scrutor.Tests/DecorationTests.cs @@ -236,6 +236,23 @@ public void Issue148_Decorate_IsAbleToDecorateConcreateTypes() Assert.NotNull(inner.Dependency); } + [Fact] + public void Issue235_Decorate_IsAbleToDecorateClassesThatTheirConstructorDoesNotContainTheDecoratedTypeDirectly() + { + var provider = ConfigureProvider(services => + { + services.AddScoped() + .AddScoped(x => x.GetRequiredService()) + .Decorate(); + }); + + using (var scope = provider.CreateScope()) + { + var result = scope.ServiceProvider.GetService(); + Assert.NotNull(result); + } + } + #region Individual functions tests [Fact] @@ -470,6 +487,16 @@ public Decorator2(DecoratedService decoratedService) public DecoratedService Inner { get; } } + public class Decorator3 : IDecoratedService + { + public Decorator3(Decorated inner) + { + Inner = inner ?? throw new ArgumentNullException(nameof(inner)); + } + + public Decorated Inner { get; } + } + public interface IService { } private class SomeRandomService : IService { }