2
Someone helps me, I am creating a project and in the dependency injection part I am having the following error when I make a Postman request in my Api: An error occurred when trying to create a 'Personal Controller' type controller. Check if the driver has a public constructor without parameters.
My Simple Injector Class:
using MonitoramentoEscolar.Domain.Interfaces.Entities;
using MonitoramentoEscolar.Domain.Entities;
using MonitoramentoEscolar.Domain.Interfaces;
using MonitoramentoEscolar.Context;
using MonitoramentoEscolar.Infrastructure.Repositories;
using SimpleInjector;
namespace MonitoramentoEscolar.Api.App_Start {
public class SimpleInjectorRegistrar {
public static Container Registrar() {
var container = new Container();
container.RegisterSingleton<MonitoramentoEscolarDbContext>();
container.Register<IPessoaEntity, PessoaEntity>();
container.Register<IPessoaTipoEntity, PessoaTipoEntity>();
container.Register<IPessoaRepository, PessoaRepository>();
container.Verify();
return container;
}
}
}
My controller, if I create an empty constructor without parameters it initializes only the empty constructor and when it arrives at the Nullreference error Endpoint.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.UI.WebControls;
namespace MonitoramentoEscolar.Api.Controllers {
public class PessoaController : ApiController {
public readonly IPessoaRepository pessoaRepository;
public readonly IPessoaTipoRepository pessoaTipoRepository;
public PessoaController(IPessoaRepository pessoaRepository, IPessoaTipoRepository pessoaTipoRepository) {
this.pessoaRepository = pessoaRepository;
this.pessoaTipoRepository = pessoaTipoRepository;
}
public PessoaController() {
}
[HttpGet]
[Route("api/Pessoa/GetAll")]
public IHttpActionResult GetAll() {
var pessoas = pessoaRepository.GetAll().ToList();
if (pessoas == null)
return BadRequest("Nenhum dado foi encontrado");
return Content(HttpStatusCode.OK, pessoas);
}
The class Simpleinjectorregister:
using MonitoramentoEscolar.Api.App_Start;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace MonitoramentoEscolar.Api {
public class WebApiApplication : HttpApplication {
protected void Application_Start() {
DependencyResolver.SetResolver(SimpleInjectorRegistrar.Registrar());
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
Someone help me and if possible explain to me why I’m going through it. Thank you.
Where is the Simpleinjector class called? What is the framework? . Net Core? . Net Full Framework?
– Renan
Your entities have a constructor without parameters?
– Leandro Angelo