Unexpected Torn Lifestyle warning when mixing Singletons and Collection
See original GitHub issueDescribe the bug
When types have been registered as singleton and those same types are registered into a collection, adding a type that depends on the collection results in a torn lifestyle when iterating the collection in the constructor. (This would appear to be related to changes for #554)
Expected behavior
When the collection is resolved, the singletons are returned as members of the collection, and it is thus OK to iterate them in the constructor of another singleton - same lifestyle.
Actual behavior
container.Verify() gives Torn Lifestyle errors:
-[Torn Lifestyle] The registration for I maps to the same implementation and lifestyle as the registration for A does. They both map to A (Singleton). This will cause each registration to resolve to a different instance: each registration will have its own instance. -[Torn Lifestyle] The registration for A maps to the same implementation and lifestyle as the registration for I does. They both map to A (Singleton). This will cause each registration to resolve to a different instance: each registration will have its own instance. -[Torn Lifestyle] The registration for I maps to the same implementation and lifestyle as the registration for B does. They both map to B (Singleton). This will cause each registration to resolve to a different instance: each registration will have its own instance. -[Torn Lifestyle] The registration for B maps to the same implementation and lifestyle as the registration for I does. They both map to B (Singleton). This will cause each registration to resolve to a different instance: each registration will have its own instance.
To Reproduce
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SimpleInjector;
public interface I { }
public class A : I { }
public class B : I { }
public class U
{
private I[] items;
public U(IEnumerable<I> items)
{
this.items = items.ToArray();
}
}
class Program
{
static async Task Main(string[] args)
{
Container container = new Container();
container.RegisterSingleton<U>();
var types = container.GetTypesToRegister(
typeof(I),
typeof(I).Assembly);
foreach(var type in types)
container.RegisterSingleton(type, type);
container.Collection.Register<I>(types);
container.Verify();
}
}
Additional context
This was working with SimpleInjector 4.0, I’m trying to upgrade to latest 4.7.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6
Confirmed, the bug is fixed in v4.8.1. Thank-you for resolving this issue.
This bug has been fixed in v4.8.1.