Skip to content

Commit

Permalink
Fixes #1108 - Stop secondary invocation of OnActivating() handlers wi…
Browse files Browse the repository at this point in the history
…th decorated instance
  • Loading branch information
tillig authored Apr 29, 2020
2 parents 865fc7b + bf902af commit 8790ac6
Show file tree
Hide file tree
Showing 3 changed files with 389 additions and 9 deletions.
3 changes: 0 additions & 3 deletions src/Autofac/Core/Resolving/InstanceLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ public object Execute()
_activationScope,
resolveParameters);

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

var handler = InstanceLookupEnding;
handler?.Invoke(this, new InstanceLookupEndingEventArgs(this, NewInstanceActivated));

Expand Down
86 changes: 86 additions & 0 deletions test/Autofac.Specification.Test/Features/DecoratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,92 @@ public void ResolvesDecoratedServiceWhenSingleDecoratorRegistered()
Assert.IsType<ImplementorA>(instance.Decorated);
}

[Fact]
public void ResolvesDecoratedServiceWhenTargetHasOnActivatingHandlerOnType()
{
var activatingInstances = new List<object>();

var builder = new ContainerBuilder();

builder.RegisterType<ImplementorA>().As<IDecoratedService>()
.OnActivating(args => activatingInstances.Add(args.Instance));
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
var container = builder.Build();

var instance = container.Resolve<IDecoratedService>();

Assert.IsType<DecoratorA>(instance);
Assert.IsType<ImplementorA>(instance.Decorated);

Assert.Single(activatingInstances);
Assert.IsType<ImplementorA>(activatingInstances[0]);
}

[Fact]
public void ResolvesDecoratedServiceWhenTargetHasOnActivatingHandlerOnInterface()
{
var activatingInstances = new List<object>();

var builder = new ContainerBuilder();

builder.RegisterType<ImplementorA>().AsSelf();
builder.Register<IDecoratedService>(c => c.Resolve<ImplementorA>())
.OnActivating(args => activatingInstances.Add(args.Instance));
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
var container = builder.Build();

var instance = container.Resolve<IDecoratedService>();

Assert.IsType<DecoratorA>(instance);
Assert.IsType<ImplementorA>(instance.Decorated);

Assert.Single(activatingInstances);
Assert.IsType<ImplementorA>(activatingInstances[0]);
}

[Fact]
public void ResolvesDecoratedServiceWhenTargetHasOnActivatedHandlerOnType()
{
var activatedInstances = new List<object>();

var builder = new ContainerBuilder();

builder.RegisterType<ImplementorA>().As<IDecoratedService>()
.OnActivated(args => activatedInstances.Add(args.Instance));
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
var container = builder.Build();

var instance = container.Resolve<IDecoratedService>();

Assert.IsType<DecoratorA>(instance);
Assert.IsType<ImplementorA>(instance.Decorated);

Assert.Single(activatedInstances);
Assert.IsType<ImplementorA>(activatedInstances[0]);
}

[Fact]
public void ResolvesDecoratedServiceWhenTargetHasOnActivatedHandlerOnInterface()
{
var activatedInstances = new List<object>();

var builder = new ContainerBuilder();

builder.RegisterType<ImplementorA>().AsSelf();
builder.Register<IDecoratedService>(c => c.Resolve<ImplementorA>())
.OnActivated(args => activatedInstances.Add(args.Instance));
builder.RegisterDecorator<DecoratorA, IDecoratedService>();
var container = builder.Build();

var instance = container.Resolve<IDecoratedService>();

Assert.IsType<DecoratorA>(instance);
Assert.IsType<ImplementorA>(instance.Decorated);

Assert.Single(activatedInstances);
Assert.IsType<ImplementorA>(activatedInstances[0]);
}

[Fact]
public void StartableTypesCanBeDecorated()
{
Expand Down
Loading

0 comments on commit 8790ac6

Please sign in to comment.