本文介绍了如何解决错误:类型不出现实施microsoft.practices.servicelocation.iservicelocator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的MVC,我下面 PRO ASP.NET MVC 4亚当·弗里曼。我目前的工作在其第六章。在我正在学习如何使用 Ninject在MVC 4 作为依赖注入。我曾在书中描述创建的应用程序。现在,我没有得到,为什么下面的错误出现:

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:

的类型不会出现执行microsoft.practices.servicelocation.iservicelocator

下面是我的控制器code:

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);
    }
}

我创建了一个名为NinjectDependencyResolver,如下级​​:

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);
    }
}

对DependencyResolver.SetResolver(新NinjectDependencyResolver());此行我的正在错误:

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.

参数名:commonServiceLocator

Parameter name: commonServiceLocator

请帮我,我怎么能解决这个错误。

Please help me, how can i resolve this error.

先谢谢了。

推荐答案

的问题是,你的 NinjectDependencyResolver 不执行的IDependencyResolver 接口,但是从 DependencyResolver 类继承。在 DependencyResolver 不执行的IDependencyResolver ,这将导致自己的方法是无关的任何MVC知道了。

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.

只需更改为:

public class NinjectDependencyResolver : IDependencyResolver

但作为UfukHacıoğulları说,你可以使用官方Ninject.MVC3的NuGet包Ninject与MVC集成。此包由Ninject的开发人员创建的,并且依赖于Ninject核心库。

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.

这篇关于如何解决错误:类型不出现实施microsoft.practices.servicelocation.iservicelocator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:23