asp.net 该类型似乎没有实现Microsoft.Practices.ServiceLocation. IServiceLocation

xriantvc  于 12个月前  发布在  .NET
关注(0)|答案(4)|浏览(131)

我是MVC的新手,我正在学习“PRO ASP.NET MVC 4 by Adam Freeman"。我目前正在学习它的第6章。在其中我正在学习如何在MVC 4中使用Ninject进行依赖注入。我已经按照书中描述的那样创建了应用程序。现在我不明白为什么会出现以下错误:
类型NinjectDependencyResolver似乎没有实现Microsoft.Practices.ServiceLocation. IServiceLocation
下面是我的控制器代码:

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());”这一行我得到错误:
类型MatrialTools.Infrastructure.NinjectDependencyResolver似乎未实现Microsoft. Practices. ServiceLocation. IServiceLocator。参数名称:commonServiceLocator
请帮助我,我如何解决这个错误。
先谢了。

ds97pgxw

ds97pgxw1#

一年后,我解决了同样的问题.感谢PDB的回答,我可以找到一个解决方案。在定制的NinjectDependencyResolver中强制使用System.Web.Mvc.IDependencyResolver而不是System.Web.Http.Dependencies.IDependencyResolver会导致强制转换问题,因为其他代码部分需要System.Web.Http.Dependencies.IDependencyResolver。例如,当你试图泛化定制的DI时:

GlobalConfiguration.Configuration.DependencyResolver =
    new NinjectDependencyResolver(kernel)

字符串
在我的例子中,我实现了IDependencyResolver,它的工作方式是这样的:

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

8dtrkrch

8dtrkrch2#

问题是你的NinjectDependencyResolver没有实现IDependencyResolver接口,而是继承了DependencyResolver类。DependencyResolver没有实现IDependencyResolver,这导致你自己的方法与MVC知道的任何东西都不相关。
只需更改为:

public class NinjectDependencyResolver : IDependencyResolver

字符串
但正如Ufuk Hacıoiullarnik所说,你可以使用官方的Ninject.MVC3 NuGet包来将Ninject与MVC集成。这个包是由Ninject的开发人员创建的,依赖于Ninject核心库。

rslzwgfq

rslzwgfq3#

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

7nbnzgx9

7nbnzgx94#

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

相关问题