我尝试将依赖项注入控制器的构造函数中,就像我之前一样。问题是我似乎无法让它发挥作用。在使用MVC 4进行DI或者我缺少一些基本要求时是否有任何新要求?
我正在使用:
Ninject.MVC3 3.0.0.6
System.Web.MVC 4.0.0.0
以下是此事的重要部分:
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
);
}
}
财务主任:
public class EventController : Controller{
private readonly EventHandler eventDtoHandler;
public EventController(EventHandler eventDtoHandler){
this.eventDtoHandler = eventDtoHandler;
}
...
Some Actions
...
}
我明白了:
[MissingMethodException:没有为此对象定义无参数构造函数。] System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,Boolean publicOnly,Boolean noCheck,Boolean&amp; canBeCached,RuntimeMethodHandleInternal&amp; ctor,Boolean&amp; 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:尝试创建'Web.Controllers.EventController'类型的控制器时发生错误。确保控制器具有无参数的公共构造函数。] System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext,Type controllerType)+182 System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext,Type controllerType)+80
在我看来,Ninject没有得到ControllerFactory的责任..