Check if the driver has a public constructor without parameters

Asked

Viewed 663 times

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?

  • Your entities have a constructor without parameters?

1 answer

2


May be one of the following options:

  • Your service makes use of other interfaces that have not been injected
  • Your service has no constructor

More:

and last you don’t need to create empty constructor in a controller

 public PessoaController() {

        }
  • 1

    Thank you very much, I managed to solve the problem, I missed adding in Nuget the Simpleinjector Webapi, register a missing interface and started receiving in the Register Httpconfiguration method, it was like this: public Static void Register(Httpconfiguration config) and I used these two lines of code more: container.Registerwebapicontrollers(config); after Verify(); I used tb: Globalconfiguration.Configuration.Dependencyresolver = new Simpleinjectorwebapidependencyresolver(container);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.