The type does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator
Asked Answered
G

4

20

I am new to MVC, i am following "PRO ASP.NET MVC 4 by Adam Freeman". I am currently working on its 6th chapter. In which i am learning how to use Ninject in MVC 4 for Dependency Injection. I have created the application as described in the book. Now i am not getting why the following Error Comes:

the type NinjectDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator

Here is my Controller code:

public class HomeController : Controller
{
    private Product[] products = {
        new Product {Name = "Kayak", Category = "Watersports", Price = 275M},
        new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M},
        new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M},
        new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95M}
    };
    private IValueCalculator calc;
    public HomeController(IValueCalculator calcParam)
    {
        calc = calcParam;
    }
    public ActionResult Index()
    {
        ShoppingCart cart = new ShoppingCart(calc) { Products = products };
        decimal totalvalue = cart.CalculateProductTotal();
        return View(totalvalue);
    }
}

I have created a class named as "NinjectDependencyResolver" as below:

public class NinjectDependencyResolver : DependencyResolver
{
    private IKernel kernal;

    public NinjectDependencyResolver()
    {
        kernal = new StandardKernel();
        AddBindings();
    }


    public object GetService(Type serviceType)
    {
        return kernal.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return kernal.GetAll(serviceType);
    }

    private void AddBindings()
    {
        kernal.Bind<IValueCalculator>().To<LinqValueCalculator>();
    }
}

Changed the global file as below:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        
        DependencyResolver.SetResolver( new NinjectDependencyResolver());
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }
}

on the "DependencyResolver.SetResolver( new NinjectDependencyResolver());" this line of i am getting the error:

The type EssentialTools.Infrastructure.NinjectDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator. Parameter name: commonServiceLocator

Please help me, how can i resolve this error.

Thanks in advance.

Golightly answered 28/8, 2013 at 11:4 Comment(6)
You can also use Ninject.MVC3 package which is easier to integrate. It supports MVC 4.Boyce
@UfukHacıoğulları why? is there any problem with "Ninject Ioc container for .NET"Golightly
Nope, it uses the same Ninject package. It simplifies the integration to an MVC application.Boyce
does the above error due that? @UfukHacıoğullarıGolightly
Nope, you are not using that package. You are integrating it manually.Boyce
@UfukHacıoğulları thanks i made a silly mistake. by the thank you very muchGolightly
B
17

The problem is that your NinjectDependencyResolver doesn't implement the IDependencyResolver interface, but inherits from the DependencyResolver class. The DependencyResolver does not implement IDependencyResolver and this causes your own methods to be unrelated to anything MVC knows.

Just change to:

public class NinjectDependencyResolver : IDependencyResolver

But as Ufuk Hacıoğulları says, you can use the official Ninject.MVC3 NuGet package to integrate Ninject with MVC. This package is created by the developers of Ninject and depends on the Ninject core library.

Bushey answered 28/8, 2013 at 11:39 Comment(3)
Thanks dude i will.. use NinJect MVC3 tooGolightly
I'm using Pro ASP.NET MVC 5, the most current version of the book (published in 2013). It does use the parent class of IDependencyResolver, as shown in Steven's answer. Unfortunately for me, I missed it when entering the code, and ended up on this SO question :-).Fraser
In the class NinjectDependencyResolver also make sure you are referencing System.Web.Mvc, I was using by mistake System.Web.Http.Dependencies as it was the suggestion that I got from Resharper.Malfeasance
S
18

One year after, I encountred the same problem... Thanks to pdb's answer, I could find a work-around. Forcing System.Web.Mvc.IDependencyResolver instead of System.Web.Http.Dependencies.IDependencyResolver in the customized NinjectDependencyResolver caused cast problems in cases other parts of code need the System.Web.Http.Dependencies.IDependencyResolver. For example when you try to generalize the customized DI :

GlobalConfiguration.Configuration.DependencyResolver =
    new NinjectDependencyResolver(kernel)

In my case, I implemented the both IDependencyResolver and it worked like this :

public class NinjectDependencyResolver
    : NinjectDependencyScope
    , IDependencyResolver
    , System.Web.Mvc.IDependencyResolver
Succubus answered 10/7, 2014 at 23:27 Comment(1)
Exact problem I had! Thank you, this is a great solution and worked perfectly for me.Hodosh
B
17

The problem is that your NinjectDependencyResolver doesn't implement the IDependencyResolver interface, but inherits from the DependencyResolver class. The DependencyResolver does not implement IDependencyResolver and this causes your own methods to be unrelated to anything MVC knows.

Just change to:

public class NinjectDependencyResolver : IDependencyResolver

But as Ufuk Hacıoğulları says, you can use the official Ninject.MVC3 NuGet package to integrate Ninject with MVC. This package is created by the developers of Ninject and depends on the Ninject core library.

Bushey answered 28/8, 2013 at 11:39 Comment(3)
Thanks dude i will.. use NinJect MVC3 tooGolightly
I'm using Pro ASP.NET MVC 5, the most current version of the book (published in 2013). It does use the parent class of IDependencyResolver, as shown in Steven's answer. Unfortunately for me, I missed it when entering the code, and ended up on this SO question :-).Fraser
In the class NinjectDependencyResolver also make sure you are referencing System.Web.Mvc, I was using by mistake System.Web.Http.Dependencies as it was the suggestion that I got from Resharper.Malfeasance
B
6

I made a similar mistake at the same point. I had implemented IDependencyResolver and got the identical error. It was caused by the wrong 'using' statement - there is a similar IDependencyResolver in System.Web.Http. Check you are using System.Web.Mvc.

Bidden answered 30/1, 2014 at 14:53 Comment(0)
F
0

The issue can be solved by implementing the IDependencyResolver interface on the StructureMapDependencyResolver class.

Fictive answered 15/9, 2017 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.