我的问题很简单! 如何手动解决此注册界面:
services.AddScoped<ICommandHandler<Command>, AddProductCommandHandler>();
此代码无效,并且始终返回null
:
我的DiContainer
是这样的:
public class ProviderDiContainer : IDiContainer
{
private readonly ServiceProvider _container;
public ProviderDiContainer(ServiceProvider container)
{
_container = container;
}
public T Resolve<T>()
{
return _container.GetService<T>();
}
}
已注册的人:
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));
}
我的服务定位器是:
public class ServiceLocator
{
public ServiceLocator(IDiContainer container)
{
Current = container;
}
public static IDiContainer Current
{
get;
private set;
}
}
现在,当我调用此代码时,它会向我返回null
:
var commandHandler = ServiceLocator.Current.Resolve<ICommandHandler<TCommand>>();
注意:首先要解决ServiceLocator
和DbiContainer
的问题,我将它们放在StartUp的Configure
方法中
答案 0 :(得分:1)
简短的回答:那是因为您要构建另一个ServiceProvider
,它不同于默认的。请注意以下代码:
var diContainer = new ProviderDiContainer(services.BuildServiceProvider());
。
这意味着您将使用服务集合来构建新的ServiceProvider
,然后将该ServiceProvider
传递给ProviderDiContainer
。 因此,ProviderDiContainer
将无法解决该行下方注册的任何服务。
如何修复
public class ProviderDiContainer: IDiContainer {private readonly ServiceProvider _container;private readonly IServiceProvider _container;public ProviderDiContainer(ServiceProvider container)public ProviderDiContainer(IServiceProvider container) { _container = container; } public T Resolve<T>() { return _container.GetService<T>(); } }
ServiceProvider
:var diContainer = new ProviderDiContainer(services.BuildServiceProvider());services.AddSingleton<IDiContainer>(c => diContainer);...services.AddSingleton(s => new ServiceLocator(diContainer));services.AddSingleton<IDiContainer>(sp => new ProviderDiContainer(sp)); services.AddSingleton<ServiceLocator>();