ASP.NET MVC 4 + Ninject MVC 3 = No Parameterless Constructor Defined for This Object

ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object

Well, I don't have an exact answer why the error is coming up, but I do know who is causing it and that is Visual Studio 2012. I installed Visual Studio 2010 on the same machine as 2012, installed ASP.NET MVC 4 for 2010 and I recreated the 2012 project into 2010 word for word, letter for letter. The final result is that when 2010 debugs the project everything works fine and Ninject injects the dependencies as it should.

When 2012 debugs its project it just comes up with the No parameterless constructor defined for this object exception. Re-targeting between .NET 4.0 and .NET 4.5 in 2012 doesn't do anything. Re-installing Ninject from NuGet also doesn't do anything. I even configured both 2010 and 2012 projects to use the local IIS server to be absolutely sure and the end result is the same.

I'm going to assume that there's a bug with Visual Studio 2012 or with Ninject. The only difference I've got between the two projects is which IDE they're running from and the 2012 project is the one that's crashing so that's why I'm pointing the finger at Visual Studio 2012.

UPDATE

Guys. GUYS! I ran into this problem AGAIN, and found the solution in another SO question: Ninject + MVC3 is not injecting into controller.

Basically, this is what's missing from the Web.config which makes it work:

<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>

I'm guessing this forces the framework to be aware of IoC containers which allows Ninject the finally be able to bind. Although, I can't help but think that the Ninject NuGet package should look for the existence of that binding redirect in the Web.config and auto-magically add it. It sure would help with a lot of hair pulling happening over this issue.

P.S. Up-vote the snot out of that post I linked because it deserves it!

No parameterless constructor defined for this object for MVC Controller

mvc needs parameterless contructions.
If you have no constructor, .Net-runtime create parameterless contruction automaticly, otherwise if you have a contruction with parameter, than the runtime don't do that and raise an error.

Ether you create a new carService on each controler, or you use a serviceProvider.
The unittest are simple with a serviceprovider.
With asp.net core its easy enter link description here, if you use mvc 4 or 5. you can see here a see here a sample enter link description here

MVC:No parameterless constructor defined for this object

You need to include below constructor to your controller,

        public HomeController() : this(new CompanyService())
{
}

So your entire controller code looks like below,

    public class HomeController : Controller
{
private ICompanyService icompanyService;

public HomeController() : this(new CompanyService())
{
}
public HomeController(ICompanyService icompanyService)
{
this.icompanyService = icompanyService;
}
public ActionResult Index()
{
ViewBag.CompanyName = this.icompanyService.GetCompany();

return View();
}
}

This will solve your issue.

Happy coding!!!!!

MVC 5 Ninject - No parameterless constructor

Since BaseController doesn't have a parameter-less constructor, and its only constructor requires a ITestRepository argument, derived types are required to supply this argument by calling the base constructor.

Try this:

public class HomeController : ControllerBase
{
public HomeController(ITestRepository testRepository) : base(testRepository)
{

}
}

This way, Ninject is able to inject ITestRepository implementation into the constructor of HomeController (the derived type) which in turn calls the base constructor (using base()) passing the required dependency.

See MSDN

MVC4 and the No parameterless constructor defined for this object. error using Unity to inject dependencies?

I have to give props to Steven, since he steered me in the right direction, but basically, I was using a method to register my resolver with the ApiController rather than using the SetResolver method to register it with a standard controller.

MVC5 and Ninject:Parameterless constructor error

You need a DependencyResolver registered in Global.asax.cs

Registration would look something like this:

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
RegisterDependencyResolver();
}

private void RegisterDependencyResolver()
{
var kernel = new StandardKernel();

// you may need to configure your container here?
RegisterServices(kernel);

DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}

And the DependencyResolver

public class NinjectDependencyResolver : IDependencyResolver
{
private readonly IKernel kernel;

public NinjectDependencyResolver(IKernel kernel)
{
this.kernel = kernel;
}

public object GetService(Type serviceType)
{
return this.kernel.TryGet(serviceType);
}

public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return this.kernel.GetAll(serviceType);
}
catch (Exception)
{
return new List<object>();
}
}
}


Related Topics



Leave a reply



Submit