1
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
he 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 briefcase 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 just one of the entities for example, this is the first time I’m doing an application like this and I think I’m missing something, in case 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 to do tests.
in that code does not have the
IPessoaServico
recorded in the methodRegisterServices
?– novic
Yes, there are all the entities , I put only the first example. but the rest of the entities are registered in the same way including Personal
– William Cézar
To avoid confusion the code has to match the other code, avoid the people find what I found, I will elaborate a test, which is the version of your application ASP.NET ?
– novic
MVC version = 5.2.3.0 am using Visual Studio 2015
– William Cézar
Microsoft.AspNet.Webapi. 5.2.3
– William Cézar
Are you making injection into Webapi and also Web? You need it to work in both?
– novic
In the future I will make the Web api , but now it is only web itself, I have not added the web api package only the MVC5
– William Cézar
Let’s go continue this discussion in chat.
– novic