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!

Steven
  • 166,672
  • 24
  • 332
  • 435
ManirajSS
  • 2,295
  • 5
  • 26
  • 50
  • Have you read the [Simple Injector ASP.NET Core MVC integration guide](https://simpleinjector.readthedocs.io/en/latest/aspnetintegration.html)? It explains how to integrate Simple Injector with ASP.NET Core MVC. – Steven Sep 19 '17 at 14:28
  • Possible duplicate of: https://stackoverflow.com/questions/37813721/asp-net-core-dependencyresolver – Steven Sep 19 '17 at 14:34
  • 2
    Also make sure you read this: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection – Steven Sep 19 '17 at 14:34

0 Answers0