.NET Core Interview Questions on Service Lifetimes
A dependency-injection lifetime tells the container how long to reuse an instance. Transient means a new resolution normally gets a new instance. Scoped means reuse within one scope. Singleton means reuse from the same container for its lifetime.
By Ravindra Wadile · Published

568 words · About 3 min read
A dependency-injection lifetime tells the container how long to reuse an instance. Transient means a new resolution normally gets a new instance. Scoped means reuse within one scope. Singleton means reuse from the same container for its lifetime.
For ASP.NET Core requests, scoped services normally live in the request scope. That does not mean every background operation automatically receives a request scope. These .NET Core interview questions focus on that boundary.
How can you observe scoped reuse?
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddScoped<Marker>();
using var provider = services.BuildServiceProvider(
new ServiceProviderOptions { ValidateScopes = true });
Guid first;
using (var scope = provider.CreateScope())
{
var a = scope.ServiceProvider.GetRequiredService<Marker>();
var b = scope.ServiceProvider.GetRequiredService<Marker>();
if (!ReferenceEquals(a, b)) throw new Exception("Scope mismatch");
first = a.Id;
}
using (var scope = provider.CreateScope())
{
var next = scope.ServiceProvider.GetRequiredService<Marker>();
if (next.Id == first) throw new Exception("Expected another scope");
}
sealed class Marker { public Guid Id { get; } = Guid.NewGuid(); }
This console example needs Microsoft.Extensions.DependencyInjection, or the relevant shared framework reference. The IDs make the demonstration readable; object identity is the actual reuse check. Two separate containers would also have separate singleton instances.
What is a captive dependency?
A long-lived service can accidentally retain a shorter-lived dependency. For example, a singleton holding a request-scoped data session would keep that session beyond the request it was meant to serve. Scope validation helps catch direct misuse with the built-in container.
Changing the data session to singleton merely to remove an error is not a safe fix. Its mutable state, disposal and concurrency behavior still matter. Choose the lifetime according to the service's responsibility.
How should a background worker obtain scoped work?
Inject IServiceScopeFactory into the worker. Create a scope for one unit of work, resolve the scoped service from that scope, await the work, then dispose the scope. For asynchronous disposal, use CreateAsyncScope with await using.
Do not cache that resolved service in the worker after the scope ends. Do not return an unawaited task that still uses the service after disposal. A scope is a lifetime boundary, not a convenient way to silence the container.
Who disposes the service?
The container disposes disposable instances it creates according to their container or scope lifetime. A caller should not manually dispose an injected service that other consumers still use. An externally created instance registered into the container has different ownership considerations; make that ownership explicit.
Transient does not automatically mean “dispose immediately after one method.” Resolution and ownership still determine when disposal happens. Avoid resolving disposable transient services repeatedly from the root provider in a long-running process.
Does singleton mean thread-safe?
No. It means shared. A singleton accessed concurrently must protect its own mutable state or avoid mutable shared state. A scoped service can also encounter concurrency if one request starts parallel work using the same instance.
Questions to ask before choosing
- Who owns the state, and when should it reset?
- Can multiple callers use this instance together?
- Does it own a disposable resource?
- Are any dependencies shorter lived?
- Is the work inside an HTTP request or an explicit background scope?
Answer with an example of reuse and disposal. That is more precise than describing every scoped service as a database connection or every singleton as a performance optimization.
References and next practice
dotnet question collection · Browse interview topics · Practise a question in Interview Challenge · How to Answer Technical Interview Questions Out Loud