激活IProductRepository时出错,使用Apress Pro ASP.NET MVC 5进行问题学习

时间:2016-01-07 17:39:10

标签: ninject

您好我正在阅读“Apress Pro ASP.NET MVC 5”这本书我已经下载了第7章,这是一个使用ninject运行的应用程序。 第一期是

1st issue snagit 我读到这是一个Ninject问题,解决了将此代码添加到SportsStoreWebUI web.config:

enter code here
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

现在,当我尝试运行App时,我得到了这个问题。

Error activating IProductRepository
No matching bindings are available, and the type is not self-bindable.

激活路径:

1)将依赖项IProductRepository注入到ProductController类型的构造函数的参数productRepository中

2)请求ProductController

建议:

1)确保您已为IProductRepository定义了绑定。

2)如果在模块中定义了绑定,请确保已将模块加载到内核中。

3)确保您没有意外创建多个内核。

4)如果使用构造函数参数,请确保参数名称与构造函数参数名称匹配。

5)如果使用自动模块加载,请确保搜索路径和过滤器正确无误。

NinjectWebCommon.cs 的内容是

    [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(SportsStore.WebUI.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(SportsStore.WebUI.App_Start.NinjectWebCommon), "Stop")]

namespace SportsStore.WebUI.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;

    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();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

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

        }        
    }
}

我已经找了但是我没有发现错误。

非常感谢。

2 个答案:

答案 0 :(得分:2)

您没有在NinjectWebCommon中添加RegisterServices:

System.Web.Mvc.DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));

答案 1 :(得分:0)

清单7-2:在NinjectWebCommon.cs文件中集成Ninject(第166页)

private static void RegisterServices(IKernel kernel) { 

System.Web.Mvc.DependencyResolver.SetResolver(new SportsStore.WebUI.Infrastructure.NinjectDependencyResolver(kernel));

}