我尝试将依赖项注入到控制器的构造函数中,就像我之前所做的那样。问题是我似乎无法让它工作。使用 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();
}
}
全球.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 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,类型控制器类型)+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 的责任。