gpt4 book ai didi

c# - 如何解决错误 :the type does not appear to implement microsoft. practices.servicelocation.iservicelocator?

转载 作者:可可西里 更新时间:2023-11-01 03:13:57 24 4
gpt4 key购买 nike

我是 MVC 的新手,我正在关注“Adam Freeman 的 PRO ASP.NET MVC 4”。我目前正在研究它的第 6 章。我正在学习如何使用 MVC 4 中的 Ninject 进行依赖注入(inject)。我已经按照书中的描述创建了应用程序。现在我不明白为什么会出现以下错误:

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

这是我的 Controller 代码:

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 NinjectDependencyResolver());”我收到错误的这一行:

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

参数名称:commonServiceLocator

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

提前致谢。

最佳答案

一年后,我遇到了同样的问题......感谢pdb的回答,我找到了解决方法。在自定义的 NinjectDependencyResolver 中强制使用 System.Web.Mvc.IDependencyResolver 而不是 System.Web.Http.DependencyResolver 在其他情况下会导致转换问题部分代码需要 System.Web.Http.Dependencies.IDependencyResolver。例如,当您尝试概括自定义 DI 时:

GlobalConfiguration.Configuration.DependencyResolver =
new NinjectDependencyResolver(kernel)

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

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

关于c# - 如何解决错误 :the type does not appear to implement microsoft. practices.servicelocation.iservicelocator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18486129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com