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