Why it matters
this commonly enables hidden runtime resolution and service locator behaviour.
Asking for a giant "surprise box" each time instead of a known tool means no one knows what you actually need.
DI011
constructor injection of `IServiceProvider`, `IServiceScopeFactory`, or `IKeyedServiceProvider` in normal services.
Why it matters
this commonly enables hidden runtime resolution and service locator behaviour.
Asking for a giant "surprise box" each time instead of a known tool means no one knows what you actually need.
Install
dotnet add package DependencyInjection.Lifetime.Analyzers --version 2.2.2
README problem example
public sealed class MyService
{
public MyService(IServiceProvider provider) { }
}
README better pattern
Repo sample extraction
Sample app warning case
public class ServiceProviderInjectionExample
{
private readonly IServiceProvider _provider;
public ServiceProviderInjectionExample(IServiceProvider provider)
{
_provider = provider;
}
public void DoWork()
{
var service = _provider.GetService<ITransientService>();
}
}
Sample app safe pattern
public class ExplicitDependencyExample
{
private readonly ITransientService _service;
public ExplicitDependencyExample(ITransientService service)
{
_service = service;
}
}
Related guides
More documentation