question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected Torn Lifestyle warning when mixing Singletons and Collection

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
danielcrabtreecommented, Dec 8, 2019

Confirmed, the bug is fixed in v4.8.1. Thank-you for resolving this issue.

0reactions
dotnetjunkiecommented, Nov 30, 2019

This bug has been fixed in v4.8.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Iterating collection with scoped dependencies in singleton ...
Iterating collection with scoped dependencies in singleton ... Unexpected Torn Lifestyle warning when mixing Singletons and Collection #769.
Read more >
Jared The Protectors 2 Teresa Gabelman
Jane Singleton stumbled her way into the perfect nannying job. She cared for the sweetest little boy, adding a sense of ordinary into...
Read more >
Cost of benicar hct without insurance
Women with a singleton pregnancy (ASPIRIN): a randomised, double-blind, placebo-controlled cost of benicar hct without insurance trial benicar with hctz.
Read more >
Should You Be Concerned With redT energy plc's (LON ...
Assessing redT energy plc's (LON:RED) performance as a company requires looking at more than just a years' earnings data.
Read more >
Tips for shopping overseas: 10 ways to avoid getting ripped off ...
Still, that doesn't mean you have to get ripped off. There are certain rules to follow to ensure you always get the best...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found