Skip to content

Adding decorator moves OnActivated event invocation #1361

@srogovtsev

Description

@srogovtsev

Given the following service/container setup:

public class OuterService
{
    public OuterService(IMiddleService middle) => Console.WriteLine("Outer");
}

public interface IMiddleService {}

public class MiddleService : IMiddleService
{
    public MiddleService(InnerService inner) => Console.WriteLine("Middle");
}

public class InnerService
{
    public InnerService() => Console.WriteLine("Inner");
}

builder.RegisterType<OuterService>().SingleInstance();
builder.RegisterType<MiddleService>().SingleInstance().As<IMiddleService>();
builder
    .RegisterType<InnerService>()
    .SingleInstance()
    .OnActivated(_ => Console.WriteLine("Inner activated"))
    ;

I observe the following output on resolving OuterService:

Inner
Middle
Outer
Inner activated

Note that activation event fires last (which allows us, for example, resolve circular dependency, were Inner to depend on Outer).

Now we add a decorator (everything else stays the same):

public class Decorator : IMiddleService
{
    public Decorator(IMiddleService inner) => Console.WriteLine("Decorator");
}

builder.RegisterDecorator<Decorator, IMiddleService>();

The output is:

Inner
Middle
Decorator
Inner activated
Outer

Now the activation event fires after decorator, not the last in the pipeline (which in our case brings back circular dependency).

Observed on Autofac 6.2 and 6.5.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions