Dependency Injection with Ninject C#
I am developing an application that has 4 layers Dominio, Infra, Servico e Web
when I have access to a route for example Home/Index
it says that there is no constructor without parameters, my Constructor is like this:
public HomeController(IPessoaServico pessoaServico)
{
this.pessoaServico = pessoaServico;
}
I have a class NinjectWebCommon.cs
in the folder App_Start
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 put only one of the entities for example, this and the first time I'm doing such an application and I think I'm missing something, in case someone can help me, in case you need more information and just talk.
EDIT: I researched a bit and saw some people indicating the installation of 2 additional Ninject MVC 5 and Ninject WEB packages.API, I am adding the 2 to do tests.
1 answers
So that the dependency injection with Ninject work in a way for both Web MVC and WebApi needs the installation of two packages:
And
After installing these two packages enter the Folder App_Start
in the file NinjectWebCommon.cs
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 row GlobalConfiguration.Configure(WebApiConfig.Register)
:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
...