My problem is simple! How can I resolve manually this registered interface:
services.AddScoped<ICommandHandler<Command>, AddProductCommandHandler>();
This code not work and always return null
:
My DiContainer
is like this:
public class ProviderDiContainer : IDiContainer
{
private readonly ServiceProvider _container;
public ProviderDiContainer(ServiceProvider container)
{
_container = container;
}
public T Resolve<T>()
{
return _container.GetService<T>();
}
}
Which is registered such:
public void ConfigureServices(IServiceCollection services)
{
var diContainer = new ProviderDiContainer(services.BuildServiceProvider());
services.AddSingleton<IDiContainer>(c => diContainer);
// Register some other things here Registrar.RegisterSomethings(services);
services.AddSingleton(s => new ServiceLocator(diContainer));
}
And my Service Locator is:
public class ServiceLocator
{
public ServiceLocator(IDiContainer container)
{
Current = container;
}
public static IDiContainer Current
{
get;
private set;
}
}
Now when I call this code it returns null
to me:
var commandHandler = ServiceLocator.Current.Resolve<ICommandHandler<TCommand>>();
Note: To resolve ServiceLocator
and DbiContainer
at first, I put them in Configure
method of the StartUp
Short answer: that' because you're building an another
ServiceProvider
which is different from the default one . Note the following code :var diContainer = new ProviderDiContainer(services.BuildServiceProvider());
.That means the you'll use the services collection to build a new
ServiceProvider
, and then pass thatServiceProvider
toProviderDiContainer
. As a result, any services registered below that line will not be resolved by theProviderDiContainer
.How to fix
ServiceProvider
: