Repository navigation
Description
Background and motivation
Many codebases that work with DI find a need for returning an empty IServiceProvider, and end up defining their own internal implementation. We could simplify this by providing a singleton instance so they don't have to.
API Proposal
namespace Microsoft.Extensions.DependencyInjection;
public sealed class EmptyServiceProvider : IKeyedServiceProvider, IServiceProviderIsKeyedService
{
public static EmptyServiceProvider Instance { get; }
// explicit implementations of the interface methods:
object? IKeyedServiceProvider.GetKeyedService(Type serviceType, object? serviceKey);
object IKeyedServiceProvider.GetRequiredKeyedService(Type serviceType, object? serviceKey);
object? IServiceProvider.GetService(Type serviceType);
bool IServiceProviderIsKeyedService.IsKeyedService(Type serviceType, object? serviceKey);
bool IServiceProviderIsService.IsService(Type serviceType);
}API Usage
public void DoWork(IServiceProvider? services = null)
{
services ??= EmptyServiceProvider.Instance;
...
}Details (added by @svick)
The service provider would not be literally empty, it would provide the services that are built into ServiceProvider: IServiceProvider, IServiceScopeFactory, IServiceProviderIsService and IServiceProviderIsKeyedService.
Alternative Designs (edited by @svick)
We could hang the singleton off of some existing type, e.g.
namespace Microsoft.Extensions.DependencyInjection;
public partial class ServiceCollection : IServiceCollection
{
public static IKeyedServiceProvider EmptyServiceProvider { get; }
}but then it's hard to add more interface implementations in a discoverable way.
An alternative name for this type is NullServiceProvider, since it follows the null object pattern. But I think the name EmptyServiceProvider is more specific and it's also more common in existing implementations.
Risks
No response
Activity
dotnet-policy-service commentedon Sep 29, 2025
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection
See info in area-owners.md if you want to be subscribed.
KalleOlaviNiemitalo commentedon Sep 30, 2025
Microsoft.Extensions.DependencyInjection.ServiceProvider has built-in services IServiceProvider, IServiceScopeFactory, IServiceProviderIsService, and IServiceProviderIsKeyedService that its IServiceProvider.GetService(Type) implementation provides even if they are not added to the IServiceCollection. Would EmptyServiceProvider do the same, or would it just return null?
wvpm commentedon Oct 19, 2025
Could you provide the context for the "need for returning an empty IServiceProvider"?
Perhaps via links to one of the "Many codebases that work with DI".
stephentoub commentedon Oct 19, 2025
https://grep.app/search?f.lang=C%23&q=class+EmptyServiceProvider
KevinCathcart commentedon Apr 28, 2026
My one comment on this, is that I'm not sure if Empty is the right name. The roughly similar thing for logging we called
NullLoggerFactory/NullLogger/NullLogger<T>. We also did Stream.Null. SoNullServiceProviderwould fit this pattern. That said, this is exactly the sort o thing that will be discussed in API review, and I don't feel strongly about the naming.KevinCathcart commentedon Apr 28, 2026
Looking at implementations in https://source.dot.net/, we do have a bunch called
EmptyServiceProvider, and only one version calledNullServiceProvider.We have one implementation that also supports
IServiceScopeFactory(resolvable) andIServiceScope. Both implemented by returningthis. I think we might want those interfaces too, to better approximate the behavior ofnew ServiceCollection().BuildServiceProvider(). I suppose in theory it could construct some other object that implements these types, but why? While the real ServiceProvider does that withIServiceProviderIsServiceandIServiceProviderIsKeyedService, but this proposal was already proposing just returningthisfor those, so that same logic makes sense for scopes too.This API shape would thus be:
For reference purposes a valid 76 line implementation that seems to fully match the behavior of
new ServiceCollection().BuildServiceProvider()(other than in terms of object identity and dispose checking) including edge cases likenullServiceKey, and returning empty IEnumerables:Expand to see implementation
svick commentedon Apr 30, 2026
@KevinCathcart I have added a note to the proposal about
NullServiceProvider.Regarding
IServiceProviderIsServiceandIServiceProviderIsKeyedService, I agree it makes sense to support them and I mentioned that in the proposal.But whether they're implemented by returning
thisis an implementation detail, so it should not be part of the API. If it's not part of the API, we can still implement it that way or change the implementation later. But if it is part of the API, it effectively has to be implemented that way. I don't see a reason to constrain the implementation like that.bartonjs commentedon May 5, 2026
Video
Added EmptyServiceProvider (#129578)
Added EmptyServiceProvider (#129578)