How to resolve error :the type does not appear to implement microsoft.practices.servicelocation.iservicelocator?

StackOverflow https://stackoverflow.com/questions/18486129

Question

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 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:

he 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.

Était-ce utile?

La solution 2

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.

Autres conseils

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

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.

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top