0
votes

Hello I'm Reading the book "Apress Pro ASP.NET MVC 5" I have downloaded chapter 7, that is a running application using ninject. The first Issue was

1st issue snagit I read it was an Ninject issue versión solved adding this code to the 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>

Now when I try to run the App I get this Issue.

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

Activation path:

1) Injection of dependency IProductRepository into parameter productRepository of constructor of type ProductController

2) Request for ProductController

Suggestions:

1) Ensure that you have defined a binding for IProductRepository.

2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.

3) Ensure you have not accidentally created more than one kernel.

4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.

5) If you are using automatic module loading, ensure the search path and filters are correct.

The Content of NinjectWebCommon.cs is

    [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)
        {

        }        
    }
}

I have looked for but I don't found the error.

Thank you very much.

2
Did you set up the binding of IProductRepository in NinjectWebCommon.cs?kayess
Ok I have put: System.Web.Mvc.DependencyResolver.SetResolver(new SportsStore.WebUI.Infrastructure.NinjectDependencyResolver(kernel)); And the ninject Issue has gone. Thanks.limonero

2 Answers

2
votes

You didn't add a RegisterServices in NinjectWebCommon:

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

Listing 7-2: Integrating Ninject in the NinjectWebCommon.cs File (pg 166)

private static void RegisterServices(IKernel kernel) { 

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

}