如何解决错误:该类型似乎未实现microsoft.practives.servicelocation.iservicel

本文关键字:实现 microsoft iservicel servicelocation practives 类型 何解决 解决 错误 | 更新日期: 2023-09-27 18:29:14

我是MVC的新手,我正在关注"Adam Freeman的PRO ASP.NET MVC 4"。我目前正在编写第六章。在这本书中,我正在学习如何在MVC 4中使用Ninject进行依赖注入。我已经创建了书中描述的应用程序。现在我不明白为什么会出现以下错误:

该类型似乎未实现microsoft.practices.servicelocation.iservicelocator

这是我的控制器代码:

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"的类,如下所示:

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

更改全局文件如下:

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(new NinjectDependencySolutionr());"这行我得到了错误:

he type EssentialTools.Infrastructure.NinjectDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator.

参数名称:commonServiceLocator

请帮助我,我该如何解决这个错误。

提前谢谢。

如何解决错误:该类型似乎未实现microsoft.practives.servicelocation.iservicel

一年后,我遇到了同样的问题。。。多亏了pdb的回答,我可以找到一份工作。如果代码的其他部分需要System.Web.Http.Dependencies.IDependencyResolver,则在自定义的NinjectDependencyResolver中强制使用System.Web.Mvc.IDependencyResolver而不是System.Web.Http.Dependencies.IDependencyResolver会导致强制转换问题。例如,当您尝试推广自定义DI:时

GlobalConfiguration.Configuration.DependencyResolver =
    new NinjectDependencyResolver(kernel)

在我的案例中,我实现了IDependencyResolver,它的工作方式如下:

public class NinjectDependencyResolver
    : NinjectDependencyScope
    , IDependencyResolver
    , System.Web.Mvc.IDependencyResolver

问题是NinjectDependencyResolver没有实现IDependencyResolver接口,而是继承自DependencyResolver类。DependencyResolver不实现IDependencyResolver,这导致您自己的方法与MVC所知道的任何东西都无关。

只需更改为:

public class NinjectDependencyResolver : IDependencyResolver

但正如Ufuk Hacıoğulları所说,你可以使用官方的Ninject.MVC3 NuGet包来将Ninject与MVC集成。该包由Ninject的开发人员创建,并依赖于Ninject核心库。

我在同一点上也犯了类似的错误。我已经实现了IDependencyResolver,并得到了相同的错误。它是由错误的"using"语句引起的——System.Web.Http中有一个类似的IDependencyResolver。检查您是否正在使用System.Web.Mvc

该问题可以通过在StructureMapDependencyResolver类上实现IDependencyResolver接口来解决。