I'm building an MVC4 app using EF5 and ninject. Something broke when I upgraded from MVC3 to 4. So I created a brand new solution, got all my nuget packages, added all my references, then copied in my code.
Project builds, thats fabulous.
My problem is the (Ninjection) sp? doesn't seem to be wiring up correctly. I get the "No Parameterless constructor defined for this object" as a runtime error when I try to load the page. However, if I simply add an empty public parameterless constructor, the page renders and all is right with the world.
My App_Start Code runs fine, NinjectWebCommon.cs (included at the bottom of the question) I've stepped through the code, but other that copying and pasting, and following tutorials online. I don't understand IoC well enough to know what to do next.
namespace search.Controllers
{
public class HomeController : Controller
{
ICamaService _service = null;
[Inject]
public HomeController(ICamaService service)
{
_service = service;
}
************** ADDING THIS FIXES THE RUNTIME ERROR *********
public HomeController(){
;
}
***********
//TODO: ADD ACTIONS
public ViewResult Index()
{
return View();
}
}
}
Here is my composition root:
[assembly: WebActivator.PreApplicationStartMethod(typeof(search4.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(search4.App_Start.NinjectWebCommon), "Stop")]
namespace search4.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using search.Services;
using search.Data;
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
public static void Stop()
{
bootstrapper.ShutDown();
}
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ICamaContext>().To<CamaContext>().InRequestScope();
kernel.Bind<ICamaService>().To<CamaService>().InRequestScope();
}
}
}
![Screen Capture of Exception][1]
http://shareimage.ro/viewer.php?file=svs5kwamqy0pxbyntig4.gif