2

I tried to inject dependencies into a controller's constructor, as i have previously. The problem is that i can't seem to get it to work. Is there any new requirement when doing DI with MVC 4 or am i missing some basic requirement ?

I'm using:
Ninject.MVC3 3.0.0.6
System.Web.MVC 4.0.0.0

Here are the important sections for that matter:


NinjectWebCommon.cs

  public static class NinjectWebCommon{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    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;
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel){
        kernel.Bind<Handling.EventHandler>().To<EventDtoHandler>();
        kernel.Bind<CommandHandler>().To<CommandDtoHandler>();
        kernel.Bind<Mapper>().To<DtoMapper>().InSingletonScope();
        kernel.Bind<Serializer>().To<DtoSerializer>();
        kernel.Bind<Deserializer>().To<DtoDeserializer>();
        kernel.Bind<CommandQueue>().To<DeviceCommandQueue>();
        kernel.Bind<Dispatcher>().To<EventDispatcher>();
        kernel.Bind<Instantiator>().To<DtoInstantiator>();
        kernel.Bind<Template>().To<NmsTemplate>().InSingletonScope();
    }   
}


Global.asax:

 public class Global : HttpApplication {

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

        RegisterRoutes(RouteTable.Routes);
    }

    public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

        routes.MapRoute(
            "Event", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Event", action = "Event", id = UrlParameter.Optional } // Parameter defaults
        );
    }
}


The Controller:

public class EventController : Controller{
    private readonly EventHandler eventDtoHandler;

    public EventController(EventHandler eventDtoHandler){
        this.eventDtoHandler = eventDtoHandler;
    }

    ...
    Some Actions
    ...
}


I get:

[MissingMethodException: No parameterless constructor defined for this object.] 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) +98 System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241 System.Activator.CreateInstance(Type type, Boolean nonPublic) +69 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +67

[InvalidOperationException: An error occurred when trying to create a controller of type 'Web.Controllers.EventController'. Make sure that the controller has a parameterless public constructor.] System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182 System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80

Seems to me that Ninject dont get the ControllerFactory responsibility..

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.