Injection of Dependency with Ninject C #

1

I am developing an application that has 4 layers Dominio, Infra, Servico e Web When I have access a route for example Home/Index it says that there is no constructor without parameters, my constructor looks like this:

public HomeController(IPessoaServico pessoaServico)
{
    this.pessoaServico = pessoaServico;
}

I have a class NinjectWebCommon.cs in the App_Start folder with the following code.

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

namespace Web.App_Start
{
    using System;
    using System.Web;
    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    using Ninject;
    using Ninject.Web.Common;
    using Infra.Banco.Interface;
    using Infra.Banco;
    using Infra.Repositorio.Interface;
    using Infra.Repositorio;
    using Servico.Interface;
    using Servico;

    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>

        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IDataBaseFactory>().To<DataBaseFactory>().InRequestScope();
            kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();

            kernel.Bind<IPessoaRepositorio>().To<PessoaRepositorio>();
            kernel.Bind<IPessoaServico>().To<PessoaServico>();
    }
}

I've just put one of the entities for example, this is the first time I'm making an application like this and I think I'm letting something go if someone can help me if you need more information and just talk.

EDIT: I searched a little and saw some people indicating the installation of 2 additional packages Ninject MVC 5 and Ninject WEB.API, I'm adding the 2 for testing.

    
asked by anonymous 21.10.2016 / 13:31

1 answer

1

So that dependency injection with Ninject works from way for both Web MVC and WebApi needs to install two packages:

  

Install-Package Ninject.MVC5 -Version 3.2.0

and

  

Install-Package Ninject.WebApi.DependencyResolver

After installing these two packages enter the App_Start folder in the NinjectWebCommon.cs file and add a line:

  

System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);

in method CreateKernel() as example layout just below:

private static IKernel CreateKernel()
{
    var kernel = new StandardKernel();
    try
    {
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
        System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = 
           new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);

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

A detail is also in Global.asax put in its first line GlobalConfiguration.Configure(WebApiConfig.Register) :

protected void Application_Start()
{
     GlobalConfiguration.Configure(WebApiConfig.Register);
     ...
21.10.2016 / 15:15