Why IDependencyResolver removed from ASP.NET Core and what should I use instead?

8.9k views Asked by At

I'm using simple injector and I couldn't find IDependencyResolver in .net core 2, is it right choice to use IServiceProvider instead of using IDependencyResolver, since there is no "IDependencyScope BeginScope()" in IserviceProvider, I am bit confused whether to use IServiceProvider.I read .net core 2.0 have better in-built DI Support but not sure about how to make use of that.

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()
    {
    }

}

Please explain to move further without IDependencyResolver and if you explain how in-built .net core 2.0 work i would more happy. Thanks!

0

There are 0 answers