Skip to main content

Posts

Showing posts with the label dependency injection

Fixing Incorrect Scope Injection of Services in .Net Core

Sometimes we inject Scope Services into singleton Services which may cause incorrect behavior of application, we will see how we can identify those objects or services Incorrect Dependency Injection Identifying and resolving issues caused by incorrect scope injection in services is crucial for maintaining the integrity and performance of a .NET Core application. Incorrectly scoped services can lead to unintended behavior, performance issues, or memory leaks. It is very important to understand Lifetime of object in .NET core and how to register with incorrect lifetime. Key rules Transient services  can be injected into any other lifetime. Scoped services  can be injected into other scoped services, or into transient services, but  not into singletons . Singleton services  can only safely inject other singletons,  not scoped or transient services . Why? Scoped services  are disposed at the end of a request. Injecting a scoped service into a singleton would me...