为什么从ASP.NET Core中删除了IDependencyResolver,我应该使用什么呢?

时间:2017-09-19 14:13:01

标签: c# dependency-injection simple-injector asp.net-core-2.0 dependency-resolver

我正在使用简单的注入器而在.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如何工作,我会更高兴。谢谢!

0 个答案:

没有答案