Infrastructure components (which are part of the composition root) such as decorators and factories, often depend on the container itself. To make it easier to write these infrastructure components, the container registers itself. This way those components can take a Container as constructor argument, and developers don’t have to explicitly register the container.
Unfortunately there is a bug in the framework, and the container is not self-registered when the overloaded constructor (that takes the ContainerOptions argument) is used.
Changing the DI configuration and passing in a ContainerOptions instance could therefore break the complete configuration.
Steps to reproduce:
[TestMethod]
public void ContainerWithOptions_ResolvingATypeThatDependsOnTheContainer_ContainerInjectsItself()
{
// Arrange
var container = new Container(new ContainerOptions());
// Act
var instance = container.GetInstance<ClassWithContainerAsDependency>();
// Assert
Assert.AreEqual(container, instance.Container);
}
public class ClassWithContainerAsDependency
{
public ClassWithContainerAsDependency(Container container) { }
}
Workaround:
Register the container manually:
var container = new Container(new ContainerOptions() { });
container.RegisterSingle(container);