c# - 没有可用的匹配绑定(bind),并且该类型不可自绑定(bind)。激活 IProductsRepository 时出错

标签 c# asp.net asp.net-mvc mocking ninject

我正在尝试学习 MVC,我是从 Adam Freeman 的Pro ASP.NET MVC 一书(第 5 版 @2013) 开始的。

在第七章中,我尝试按照书中的示例制作一个小应用程序。

应用在设置并尝试加载产品列表后加载失败。

我正在尝试创建抽象存储库 IProductRepository 的模拟实现,并让 Ninject 在收到实现 的请求时返回模拟对象>IPproductRepository 接口(interface)。

我已经搜索并查看了其他问题/答案,但没有找到任何可以帮助解决我的问题并让我继续学习的东西。这可能是基本的东西,但我真的很想知道什么以及为什么不能正常工作。

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);
            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
        }        
    }
}

接下来是我的 NinjectDependencyResolver 类:

   public class NinjectDependencyResolver : IDependencyResolver
    {

        private IKernel kernel;

        [Inject]
        public NinjectDependencyResolver(IKernel kernelParam)
        {
            kernel = kernelParam;
            AddBindings();
        }


        public object GetService(Type serviceType)
        {
            return kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return kernel.GetAll(serviceType);
        }


        private void AddBindings()
        {
            var mock = new Mock<IProductsRepository>();

            mock.Setup(m => m.Products).Returns(new List<Product>
            {
                new Product { Name = "Fotball", Price = 25 },
                new Product { Name = "Surf Board", Price = 45 },
                new Product { Name = "Running Shoes", Price = 95 }
            });

            kernel.Bind<IProductsRepository>().ToConstant(mock.Object);
        }
    }

这是我的 Controller 类:

public class ProductController : Controller
{


    private IProductsRepository repository;



    public ProductController(IProductsRepository productRepository)
    {
        repository = productRepository;
    }



    public ViewResult List()
    {
        return View(repository.Products);
    }

我得到的错误如下:

激活 IProductsRepository 时出错
没有可用的匹配绑定(bind),类型不可自绑定(bind)。
激活路径:
2) 将依赖IPproductsRepository 注入(inject)到ProductController 类型的构造函数的参数productRepository 中。
1) 请求 ProductController。
建议:
1) 确保您已经为 IProductsRepository 定义了一个绑定(bind)。
2) 如果绑定(bind)是在模块中定义的,确保模块已经加载到内核中。
3) 确保您没有意外创建多个内核。
4) 如果您使用构造函数参数,请确保参数名称与构造函数参数名称匹配。
5) 如果您使用自动模块加载,请确保搜索路径和过滤器正确。

最佳答案

好的,看来我还有另一个错误:

发现同一依赖程序集的不同版本之间存在无法解决的冲突

我已经安装了特定版本的 Ninject、Ninject.Web.Common、Ninject.MVC3、Moq 和本书作者指定的其他包。

阅读构建输出中的错误后,我尝试将所有已安装的软件包更新到最新版本,重建项目,一切正常!

关于c# - 没有可用的匹配绑定(bind),并且该类型不可自绑定(bind)。激活 IProductsRepository 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40150986/

相关文章:

c# - Visual Studio 2010 中的 Debug模式和 Release模式有什么区别?

c# - 在 SAP Business One 9 中使用 DI API 附加文件时出错

c# - 如何通过span ID访问span内的ASP控件?

ASP.NET session 状态 Sql Server 模式

c# - Entity Framework 中的 System.Data.ProviderIncompatibleException

c# - 向用户控件添加滚动条

c# - 如何在 WPF 中强制 DataGrid 组顺序?

ASP.net 4.0 实体数据模型 Mysql 未正确处理 Mysql 枚举

asp.net-mvc - URL 不同于 View 名称 ASP MVC 3

javascript在asp.net mvc中打印div内容