在LightInject 3.0.2.5中使用Fallback with Create时的StackOverflow异常

时间:2015-03-18 05:07:31

标签: c# dependency-injection inversion-of-control light-inject

这是https://github.com/seesharper/LightInject/issues/173

的副本

我尝试使用fallback和.Create()自动创建具体类型,但它以某种方式自行循环,我不明白为什么。

这是我的测试代码:

public class Foo
{
    public Foo(IBar bar, IBar2 bar2)
    {

    }
}

public interface IBar2
{
}

class Bar2 : IBar2
{
}

public interface IBar
{
}

class Bar : IBar
{
}

private ServiceContainer container = new ServiceContainer();


container.RegisterFallback((t, s) => true, Factory);
container.Register<IBar, Bar>();
container.Register<IBar2, Bar2>();
var foo = container.GetInstance<Foo>(); // Error here

private object Factory(ServiceRequest req)
{
    return container.Create(req.ServiceType);
}

你能告诉我吗?

即使Factory方法如下所示,它也会循环:

private object Factory(ServiceRequest req)
{
    container.Register(typeof(Foo));
    return container.GetInstance<Foo>();
}

但如果我事先登记Foo(我显然想避免),那就完美了。

container.Register(typeof(Foo));
var foo = container.GetInstance<Foo>(); //ok

2 个答案:

答案 0 :(得分:0)

这是https://github.com/seesharper/LightInject/issues/173中确认的错误,由作者负责

答案 1 :(得分:0)

我是LightInject的作者,该问题已更新,其解决方法使容器能够解析未注册的具体类。

https://github.com/seesharper/LightInject/issues/173