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

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

Closed
alistairjevans opened this issue Sep 28, 2020 · 2 comments · Fixed by #1205
Labels

Comments

@alistairjevans
Copy link
Member

alistairjevans commented Sep 28, 2020

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.

@tillig
Copy link
Member

tillig commented Sep 28, 2020

this is definitely just a v6 issue that wasn't in v6

I think you mean "a v6 issue that wasn't in v5." 😆 But, yeah, good find. I'm glad we're catching this stuff.

@alistairjevans
Copy link
Member Author

Fixing this one may actually be the thing that unblocks the dynamic proxy issue as well (autofac/Autofac.Extras.DynamicProxy#40)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants