
Note. Before delving into this article, I recommend you read the previous part of this article for a better understanding.
- .NET Core: A Beginner's Guide to the Fundamentals
- https://www.c-sharpcorner.com/blogs/net-core-a-beginners-guide-to-the-fundamentals-part-2
Short Note from the previous article.
- Transient: Use for lightweight, stateless services that don't maintain any internal state. Each usage results in a new instance.
- Scoped: Ideal for services that should share data and state within a single client request. Commonly used for database contexts and unit-of-work patterns.
- Singleton: Suitable for services that maintain a shared state across the entire application. Be cautious with mutable states, as changes may affect other parts of your application.
Simple Image Illustration.

Interface like below
namespace scopesandlifetimeservices.Services
{
public interface IScopedService :IDisposable
{
public Guid ScopeId { get; set; }
}
public interface ISingletonService : IDisposable
{
public Guid SingletonId { get; set; }
}
public interface ITransientService : IDisposable
{
public Guid TransientId { get; set; }
}
}
Implement the interfaces
namespace scopesandlifetimeservices.Services
{
public class ScopedService : IScopedService
{
public Guid ScopeId { get; set; }
public void Dispose() => GC.Collect();
public ScopedService()
{
ScopeId= Guid.NewGuid();
}
}
public class SingletonService : ISingletonService
{
public Guid SingletonId { get; set; }
public void Dispose() => GC.Collect();
public SingletonService()
{
SingletonId = Guid.NewGuid();
}
}
public class TransientService : ITransientService
{
public Guid TransientId { get; set; }
public void Dispose() => GC.Collect();
public TransientService()
{
TransientId = Guid.NewGuid();
}
}
}


Join the conversation! Your thoughts help the community grow.