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

0

7

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!

Choi answered 19/9, 2017 at 14:13 Comment(3)
Have you read the Simple Injector ASP.NET Core MVC integration guide? It explains how to integrate Simple Injector with ASP.NET Core MVC.Circumambulate
Possible duplicate of: #37814221Circumambulate
Also make sure you read this: learn.microsoft.com/en-us/aspnet/core/fundamentals/…Circumambulate

© 2022 - 2024 — McMap. All rights reserved.