Skip to content

RecursiveLocateException with factory injection #307

@mythteria

Description

@mythteria

Hi! I am migrating from autofac to grace ioc and ran into an issue. Suppose such a structure (simplified):

class AService
{
    private readonly Func<ADependency> _func;

    public AService(Func<ADependency> func) =>
        _func = func;

    public ADependency GetDependency() =>
        _func();
}

class ADependency
{
    public ADependency(AService a)
    {
    }
}

Autofac registration works fine:

var autofacContainerBuilder = new ContainerBuilder();
autofacContainerBuilder.RegisterType<ADependency>();
autofacContainerBuilder.RegisterType<AService>();
var autofacContainer = autofacContainerBuilder.Build();
var s1 = autofacContainer.Resolve<AService>();

Unfortunately, when i port it to grace, i got a RecursiveLocateException:

var graceContainer = new DependencyInjectionContainer();
graceContainer.Configure(c =>
{
    c.Export<ADependency>();
    c.Export<AService>(); // throws!
    // c.ExportFactory<IExportLocatorScope, AService>(scope => new AService(scope.Locate<Func<ADependency>>())); // work
});
var s2 = graceContainer.Locate<AService>();

I found a workaround (commented), but it looks rather ugly. Is there a more elegant way to solve this problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions