Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1041 - PropertiesAutowired fails when service is decorated using new syntax #1043

Merged
merged 11 commits into from
Nov 3, 2019
5 changes: 4 additions & 1 deletion src/Autofac/Core/Resolving/InstanceLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ private object Activate(IEnumerable<Parameter> parameters, out object decoratorT
{
decoratorTarget = _newInstance = ComponentRegistration.Activator.ActivateInstance(this, resolveParameters);

ComponentRegistration.RaiseActivating(this, resolveParameters, ref _newInstance);

_newInstance = InstanceDecorator.TryDecorateRegistration(
_service,
ComponentRegistration,
Expand Down Expand Up @@ -153,7 +155,8 @@ private object Activate(IEnumerable<Parameter> parameters, out object decoratorT
}
}

ComponentRegistration.RaiseActivating(this, resolveParameters, ref _newInstance);
if (_newInstance != decoratorTarget)
ComponentRegistration.RaiseActivating(this, resolveParameters, ref _newInstance);

return _newInstance;
}
Expand Down
64 changes: 64 additions & 0 deletions test/Autofac.Test/Features/Decorators/DecoratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,40 @@ private interface IService
{
}

private interface IAutoWiredService
{
bool NestedServiceIsNotNull();
}

private class NestedService
{
}

private class AutoWiredService : IAutoWiredService
{
public NestedService NestedService { get; set; }

public bool NestedServiceIsNotNull()
{
return NestedService != null;
}
}

private class AutoWiredServiceDecorator : IAutoWiredService
{
private readonly IAutoWiredService _original;

public AutoWiredServiceDecorator(IAutoWiredService original)
{
_original = original;
}

public bool NestedServiceIsNotNull()
{
return _original.NestedServiceIsNotNull();
}
}

public class Foo
{
}
Expand Down Expand Up @@ -149,6 +183,36 @@ public void RegistrationTargetsTheImplementationType()
Assert.Equal(typeof(ImplementorA), registration.Target.Activator.LimitType);
}

[Fact]
public void DecorateReflectionActivatorWithPropertyInjection()
{
var builder = new ContainerBuilder();
builder.RegisterType<NestedService>();
builder.RegisterType<AutoWiredService>().As<IAutoWiredService>().PropertiesAutowired();
builder.RegisterDecorator<AutoWiredServiceDecorator, IAutoWiredService>();

var container = builder.Build();

var service = container.Resolve<IAutoWiredService>();

Assert.True(service.NestedServiceIsNotNull());
}

[Fact]
public void DecorateProvidedInstanceActivatorWithPropertyInjection()
{
var builder = new ContainerBuilder();
builder.RegisterType<NestedService>();
builder.RegisterInstance<IAutoWiredService>(new AutoWiredService()).PropertiesAutowired();
builder.RegisterDecorator<AutoWiredServiceDecorator, IAutoWiredService>();

var container = builder.Build();

var service = container.Resolve<IAutoWiredService>();

Assert.True(service.NestedServiceIsNotNull());
}

private abstract class Decorator : IDecoratedService
{
protected Decorator(IDecoratedService decorated)
Expand Down