JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
566 stars 117 forks source link

Stackoverflow when using Inverse Registrations where there's a Bi-directional dependence #234

Closed hahn-kev closed 4 years ago

hahn-kev commented 4 years ago

Pretty simple.

public class Thing1
{
    public Thing1(Thing2 thing2) { }
}

public class Thing2
{
    public Thing2(Thing1 thing1) { }
}

static void Main(string[] args)
{
    var container = Container.For(r =>
    {
        //r.For<Thing1>().Use<Thing1>().Singleton();  <--- throws expected exception
        r.Use<Thing1>().Singleton().For<Thing1>(); //<--- crashes app
        r.AddSingleton<Thing2>();
    });
    var instance = container.GetService<Thing2>();
    Console.WriteLine("Success!");
}

I ran into an issue in my application where I had a Bi-Directional dependency without realizing it however instead of a helpful error I would just get a stackoverflow exception without any useful information. I've reproduced the issue in as small an example as I can provide, if you run it the console will just print Stack overflow. with no more information.

After some testing it seems like it only happens when using the Inverse Registration, normal registration throws the normal Bi-directional dependence exception.

jeremydmiller commented 4 years ago

Got it, and it's going into the "Coronavirus Isolation Release" (4.2)

jeremydmiller commented 4 years ago

Actually, this is gonna be a little rough to deal with for 4.2. My advice would be to avoid using the inverse registration for now.

jeremydmiller commented 4 years ago

I think in the long run I'm going to deprecate the inverse registration. It's been too buggy and I just don't want to have to support it.