为什么即使我使用 ninject 注入依赖项,我仍会收到 MissingMethodException?
Why am I getting MissingMethodException even though I inject dependencies with ninject?
当我尝试访问“考试”视图时,出现此错误。不明白,因为好像一切正常。
错误:
[MissingMethodException: No constructor without parameters has been 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, StackCrawlMark & stackMark) +122
System.RuntimeType.CreateInstanceDefaultCtor (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark & stackMark) +239
System.Activator.CreateInstance (Type type, Boolean nonPublic) +85
System.Activator.CreateInstance (Type type) +12
System.Web.Mvc.DefaultControllerActivator.Create (RequestContext requestContext, Type controllerType) +55
[InvalidOperationException: An error occurred when trying to create a controller of type 'ConsultaH.MVC.Controllers.ExamesController'. Make sure the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create (RequestContext requestContext, Type controllerType) +178
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance (RequestContext requestContext, Type controllerType) +80
System.Web.Mvc.DefaultControllerFactory.CreateController (RequestContext requestContext, String controllerName) +102
System.Web.Mvc.MvcHandler.ProcessRequestInit (HttpContextBase httpContext, IController & controller, IControllerFactory & factory) +188
System.Web.Mvc.MvcHandler.BeginProcessRequest (HttpContextBase httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.BeginProcessRequest (HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest (HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () +105
System.Web.HttpApplication.ExecuteStepImpl (IExecutionStep step) +50
System.Web.HttpApplication.ExecuteStep (IExecutionStep step, Boolean & completedSynchronously) +163
我里面的代码NinjectWebCommon.cs:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind(typeof(IAppServiceBase<>)).To(typeof(AppServiceBase<>));
kernel.Bind<IPacienteAppService>().To<PacienteAppService>();
kernel.Bind<IExameAppService>().To<ExameAppService>();
kernel.Bind<ITipoExameAppService>().To<TipoExameAppService>();
kernel.Bind<IConsultaAppService>().To<ConsultaAppService>();
kernel.Bind(typeof(IServiceBase<>)).To(typeof(ServiceBase<>));
kernel.Bind<IPacienteService>().To<PacienteService>();
kernel.Bind<IExameService>().To<ExameService>();
kernel.Bind<ITipoExameService>().To<TipoExameService>();
kernel.Bind<IConsultaService>().To<ConsultaService>();
kernel.Bind(typeof(IRepositoryBase<>)).To(typeof(RepositoryBase<>));
kernel.Bind<IPacienteRepository>().To<PacienteRepository>();
kernel.Bind<IExameRepository>().To<ExameRepository>();
kernel.Bind<ITipoExameRepository>().To<TipoExameRepository>();
kernel.Bind<IConsultaRepository>().To<ConsultaRepository>();
}
我的ExamesController.csclass:
public class ExamesController : Controller
{
private readonly IExameAppService _exameApp;
public ExamesController(IExameAppService exameApp)
{
_exameApp = exameApp;
}
// GET: Exame
public ActionResult Index()
{
var exames = _exameApp.GetAll();
var exameViewModel = Mapper.Map<IEnumerable<Exame>, IEnumerable<ExameViewModel>>(exames);
return View(exameViewModel);
}
}
我在方法 CreateKernel() class NinjectWebCommon.cs 中放置了一个断点并且总是尝试后到达第一行时出现错误,此错误发生在 运行 时间。请参阅下面的代码:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); // <= This line
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
我研究了很多,在这种情况下找不到任何解决方案。
要解决此错误,我所要做的就是卸载 Ninject 默认包并安装名为 Ninject MVC5 的包。
当我尝试访问“考试”视图时,出现此错误。不明白,因为好像一切正常。
错误:
[MissingMethodException: No constructor without parameters has been 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, StackCrawlMark & stackMark) +122 System.RuntimeType.CreateInstanceDefaultCtor (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark & stackMark) +239 System.Activator.CreateInstance (Type type, Boolean nonPublic) +85 System.Activator.CreateInstance (Type type) +12 System.Web.Mvc.DefaultControllerActivator.Create (RequestContext requestContext, Type controllerType) +55
[InvalidOperationException: An error occurred when trying to create a controller of type 'ConsultaH.MVC.Controllers.ExamesController'. Make sure the controller has a parameterless public constructor.] System.Web.Mvc.DefaultControllerActivator.Create (RequestContext requestContext, Type controllerType) +178 System.Web.Mvc.DefaultControllerFactory.GetControllerInstance (RequestContext requestContext, Type controllerType) +80 System.Web.Mvc.DefaultControllerFactory.CreateController (RequestContext requestContext, String controllerName) +102 System.Web.Mvc.MvcHandler.ProcessRequestInit (HttpContextBase httpContext, IController & controller, IControllerFactory & factory) +188 System.Web.Mvc.MvcHandler.BeginProcessRequest (HttpContextBase httpContext, AsyncCallback callback, Object state) +50 System.Web.Mvc.MvcHandler.BeginProcessRequest (HttpContext httpContext, AsyncCallback callback, Object state) +48 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest (HttpContext context, AsyncCallback cb, Object extraData) +16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () +105 System.Web.HttpApplication.ExecuteStepImpl (IExecutionStep step) +50 System.Web.HttpApplication.ExecuteStep (IExecutionStep step, Boolean & completedSynchronously) +163
我里面的代码NinjectWebCommon.cs:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind(typeof(IAppServiceBase<>)).To(typeof(AppServiceBase<>));
kernel.Bind<IPacienteAppService>().To<PacienteAppService>();
kernel.Bind<IExameAppService>().To<ExameAppService>();
kernel.Bind<ITipoExameAppService>().To<TipoExameAppService>();
kernel.Bind<IConsultaAppService>().To<ConsultaAppService>();
kernel.Bind(typeof(IServiceBase<>)).To(typeof(ServiceBase<>));
kernel.Bind<IPacienteService>().To<PacienteService>();
kernel.Bind<IExameService>().To<ExameService>();
kernel.Bind<ITipoExameService>().To<TipoExameService>();
kernel.Bind<IConsultaService>().To<ConsultaService>();
kernel.Bind(typeof(IRepositoryBase<>)).To(typeof(RepositoryBase<>));
kernel.Bind<IPacienteRepository>().To<PacienteRepository>();
kernel.Bind<IExameRepository>().To<ExameRepository>();
kernel.Bind<ITipoExameRepository>().To<TipoExameRepository>();
kernel.Bind<IConsultaRepository>().To<ConsultaRepository>();
}
我的ExamesController.csclass:
public class ExamesController : Controller
{
private readonly IExameAppService _exameApp;
public ExamesController(IExameAppService exameApp)
{
_exameApp = exameApp;
}
// GET: Exame
public ActionResult Index()
{
var exames = _exameApp.GetAll();
var exameViewModel = Mapper.Map<IEnumerable<Exame>, IEnumerable<ExameViewModel>>(exames);
return View(exameViewModel);
}
}
我在方法 CreateKernel() class NinjectWebCommon.cs 中放置了一个断点并且总是尝试后到达第一行时出现错误,此错误发生在 运行 时间。请参阅下面的代码:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); // <= This line
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
我研究了很多,在这种情况下找不到任何解决方案。
要解决此错误,我所要做的就是卸载 Ninject 默认包并安装名为 Ninject MVC5 的包。