1

Closed

Infinite loop when auto-resolving unregistered class with unresolvable dependencies

description

Code:
 
    interface IBar { }
 
    class Foo
    {
        public Foo(IBar b) { }
    }

    public void Test()
    {            
        var container = MunqDependencyResolver.Container;
        var foo = container.Resolve<Foo>(); // infinite loop!
    }
 
After stepping in, the problem is in the Resolve method. Since Foo is not resolvable, it attempts to auto-register it. It creates a delegate that looks like new Foo(Resolve<IBar>()) and registers that, then recursively calls Resolve. The problem is that Foo is still not resolvable, because IBar cannot be resolved. The auto-resolver does not distinguish between Foo being unregistered and Foo being unresolvable, and so it keeps attempting to register it until it runs out of stack space and crashes.
Closed Mar 9, 2012 at 7:50 PM by mdennis
fixed

comments

dfullerton wrote Oct 5, 2011 at 7:03 PM

This seems to fix it. On line 48 of Container.Resolver.cs:
  • return Resolve(name, type);
  • return typeRegistry.Get(name, type).GetInstance();