The term life cycle madness is a quote from Jeffrey Richter's CLR via C#, and its chapter about threading. Understanding the life cycle of dependencies is important in order to avoid performance issues in our application. Above all, we should avoid the following cases:
- Consuming scoped dependencies in a singleton consumer: As previously mentioned, a scoped life cycle means that a new instance is created for each request. When we try to consume a scoped instance in a singleton life cycle, the runtime will throw an exception as follows: InvalidOperationException: Cannot consume scoped service 'Services.MyScopedService' from singleton 'Services.MySingletonService'. This is because the runtime cannot create a scoped service ...