Skip to content

Decorated Instances do not get properties injected if they are marked with AllowCircularDependencies #1204

@alistairjevans

Description

@alistairjevans

Related to #1200, but this is definitely just a v6 issue that wasn't in v5.

If you have a decorated instance, with property injection, and circular dependencies allowed on said property injection, the properties won't be injected.

The instance used for property injection in that event is the decorator, rather than the instance.

This (new) test fails:

private interface IMyService
{
    void AssertProp();
}

private sealed class DecoratedService : IMyService
{
    public string Prop { get; set; }

    public void AssertProp()
    {
        if (Prop is null)
        {
            throw new NullReferenceException();
        }
    }
}

private sealed class ServiceDecorator : IMyService
{
    private readonly IMyService _decorating;

    public ServiceDecorator(IMyService decorating)
    {
        _decorating = decorating;
    }

    public void AssertProp()
    {
        _decorating.AssertProp();
    }
}

[Fact]
public void DecoratedInstanceWithPropertyInjectionAllowingCircularReferencesStillInjects()
{
    var val = "Value";

    var builder = new ContainerBuilder();
    builder.RegisterInstance(val);
    builder.RegisterType<DecoratedService>().As<IMyService>().PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
    builder.RegisterDecorator<ServiceDecorator, IMyService>();

    var container = builder.Build();
    var instance = container.Resolve<IMyService>();

    instance.AssertProp();
}

Luckily, unlike #1200, this one should be an easy fix. I just need to ensure I capture the new instance and pass that into the event handler.

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