No parameterless constructor defined for this object

0

I'm trying to create a parameterized constructor, as shown below:

public class TestController : Controller
{
    private readonly IAppService _servico;

    public DashboardController(IAppService servico)
    {
        _servico = servico;
    }

    public ActionResult Index()
    {
        var a = _servico.Pesquisar();
        return View();
    }
}

Dependency injection has been made, but it still returns the error:

  

No parameterless constructor defined for this object

   public static class SimpleInjectorWebApiInitializer
{

    public static void Initialize()
    {
        var container = new Container();
        container.Options.DefaultScopedLifestyle = new SimpleInjector.Lifestyles.AsyncScopedLifestyle();

        InitializeContainer(container);

        container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

        container.Verify();

        SimpleInjectorMvcExtensions.GetControllerTypesToRegister(container, Assembly.GetExecutingAssembly());
    }

    private static void InitializeContainer(Container container)
    {
        Bootstrapper.Start(container);
    }
}

I have tried several alternatives, but without success. Would you please let me know how I can resolve this problem?

Thank you in advance!

    
asked by anonymous 12.09.2018 / 15:53

2 answers

1

You did not register the dependency injection of IAppService , your code would look something like this:

public static void Initialize()
{
    var container = new Container();
    container.Options.DefaultScopedLifestyle = new SimpleInjector.Lifestyles.AsyncScopedLifestyle();

    //Registra os services
    container.Register<IAppService, AppService>(Lifestyle.Scoped);

    InitializeContainer(container); 

    container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

    container.Verify();

    SimpleInjectorMvcExtensions.GetControllerTypesToRegister(container, Assembly.GetExecutingAssembly());
}

You can see more in their documentation: link

    
12.09.2018 / 16:15
2
  

No parameterless constructor defined for this object

That is, you need to have a parameterless constructor:

public DashboardController()
{
}

This will not mess up your other interface builder where you are injecting dependency.

    
12.09.2018 / 16:03