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

有帮助吗?

解决方案

I am not a Ninject user, but from my experiences with other IOC frameworks in MVC, you would need to replace the DefaultControllerFactory with an implementation that injects objects instead of requiring a default constructor.

其他提示

Looks like your bindings arn't being registered propertly.

Im not sure exactly what's wrong, but I create a NinjectApplicationModule that works for me:

/// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        kernel.Load(new NinjectApplicationModules());
    } 


public class NinjectApplicationModules : NinjectModule
{
    /// <summary>
    /// Loads the Binding module into the kernel. Used to map Abstract Classes to Concrete classes at runtime.
    /// </summary>
    public override void Load()
    {
        // Bindings...

        Bind<ICamaContext>().To<CamaContext>().InRequestScope();
        Bind<ICamaService>().To<CamaService>().InRequestScope();

    }
}

Check you data model class.

Public Class A ()
{
   public A() {
     }
  public string Name{get; set;}
}

But you need to remove this default Class A Constructor.

 Public Class A ()
{
  public string Name{get; set;}
}

I was already facing No Parameter less constructor defined for this object

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top