simpleinjector / SimpleInjector

An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
https://simpleinjector.org
MIT License
1.21k stars 155 forks source link

StackOverflowException when using flowing scoped lifestyle and injecting a decorated interface inside a class registered as a collection #998

Closed kevenpoulincanam closed 3 months ago

kevenpoulincanam commented 3 months ago

A StackOverflowException is thrown when using flowing scoped lifestyle and registering a decorator that is injected inside a class which is injected as a collection. A bit hard to describe clearly but the steps to reproduce are simple (see below)

Behavior:

To reproduce:

internal class Program
{
    static void Main()
    {
        var container = new Container();
        container.Options.DefaultScopedLifestyle = ScopedLifestyle.Flowing;
        container.Register<IStrategy, Strategy>();
        container.RegisterDecorator<IStrategy, StrategyDecorator>();
        container.Collection.Register<IMyInterface>(typeof(MyImpl));
        container.Verify();

        Console.WriteLine("All good");
        Console.ReadLine();
    }
}

public interface IMyInterface { }
public class MyImpl : IMyInterface
{
    public MyImpl(IStrategy strategy) { }
}

public interface IStrategy { }
public class Strategy : IStrategy { }
public class StrategyDecorator : IStrategy
{
    public StrategyDecorator(IStrategy strategy) { }
}

Additional context:

dotnetjunkie commented 3 months ago

Thank you for this detailed error report. Using it I was able to successfully reproduce the issue. I'll investigate and get back to you with an intermediate workaround and later on, hopefully, a new patch release.

dotnetjunkie commented 3 months ago

I've got two solutions for you:

  1. Register the decorator as scoped or
  2. Upgrade to Simple Injector v5.4.6 where this bug is fixed.
kevenpoulincanam commented 3 months ago

Many thanks!