1
I am building an ASP.NET MVC5 application using DDD and have separated my Ioc layer from my web application. In my controllers I have constructs with parameter to receive an instance of my service class. I’m using Ninject, in which I created a class that extends Idependencyresolver, so in my Global.asax I can call the following code:
DependencyResolver.SetResolver(new App.Infra.Ioc.MyDependencyResolver());
Within the Mydependencyresolver class I make the Binds of my dependencies.
The problem is that when I call my action, for example Clientes/Index
the system returns me the error:
Nenhum construtor sem parâmetros foi definido para este objeto.
[MissingMethodException: Nenhum construtor sem parâmetros foi definido para este objeto.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +113
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
System.Activator.CreateInstance(Type type) +66
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +110
I know this error occurs because my dependency is not being mapped, but it should work, because in Global.asax I pass to Dependencyresolver my dependency injection.
I have already implemented in another project using Ninjectwebcommon, but this was within the Asp.Net MVC project. What I want is to implement this into an outside project.
How do I do that?
It would be interesting to put your Controller code in the question (Constructor, Action ...).
– Renan
In fact, the error says that you are trying to create a class (which we don’t know) using an empty constructor. If the class is yours, implementing an empty constructor already solves this specific error.
– Leonel Sanches da Silva
Ever thought about putting a constructor method without parameters in the class?
public IoC(){}
– Nivaldo Santana