我正在使用简单的注入器而在.net core 2中找不到IDependencyResolver
,使用IServiceProvider
而不是使用IDependencyResolver
是正确的选择,因为没有IDependencyScope BeginScope()
中的“IserviceProvider
”,我对是否使用IServiceProvider
感到困惑。我读过.net core 2.0有更好的内置DI支持但不确定如何使用它
public class SimpleInjectorWebApiDependencyResolver : IDependencyResolver
{
private readonly Container container;
public SimpleInjectorWebApiDependencyResolver(Container container)
{
this.container = container;
}
[DebuggerStepThrough]
public IDependencyScope BeginScope() // what should i use instead of IdependencyScope in .Net Core
{
return this;
}
[DebuggerStepThrough]
public object GetService(Type serviceType)
{
return ((IServiceProvider)this.container).GetService(serviceType);
}
[DebuggerStepThrough]
public IEnumerable<object> GetServices(Type serviceType)
{
//return this.container.GetAllInstances(serviceType);
IServiceProvider provider = this.container;
Type collectionType = typeof(IEnumerable<>).MakeGenericType(serviceType);
var services = (IEnumerable<object>)provider.GetService(collectionType);
return services ?? Enumerable.Empty<object>();
}
[DebuggerStepThrough]
public void Dispose()
{
}
}
请解释在没有IDependencyResolver
的情况下进一步移动,如果你解释内置的.net核心2.0如何工作,我会更高兴。谢谢!