ASP.NET MVC:没有为Lightinject

时间:2016-02-15 15:20:49

标签: c# asp.net-mvc constructor code-injection light-inject

我已开始使用LightInject,但收到No parameterless constructor defined for this object错误。 代码如下,

的Global.asax.cs:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);

    var container = new ServiceContainer();
    container.Register<IEmployeeRepository, EmployeeRepository>();
    container.RegisterControllers();
    container.EnableMvc();
}

控制器:

public HomeController(IEmployeeRepository repo)
{
    repository = repo;
}

任何人都可以帮助我。

堆栈追踪:

  

[MissingMethodException:没有为此定义无参数构造函数   对象。] System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,   Boolean publicOnly,Boolean noCheck,Boolean&amp; canBeCached,   RuntimeMethodHandleInternal&安培; ctor,布尔&amp; bNeedSecurityCheck)+0
  System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean   skipCheckThis,Boolean fillCache,StackCrawlMark&amp; stackMark)+113
  System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly,   Boolean skipCheckThis,Boolean fillCache,StackCrawlMark&amp; stackMark)   +232 System.Activator.CreateInstance(Type type,Boolean nonPublic)+83 System.Activator.CreateInstance(Type type)+66 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext   requestContext,Type controllerType)+110

     

[InvalidOperationException:尝试创建时出错   控制器类型&#39; TestTools.Controllers.HomeController&#39;。确保   控制器有一个无参数的公共构造函数。]
  System.Web.Mvc.DefaultControllerActivator.Create(的RequestContext   requestContext,Type controllerType)+247
  System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(的RequestContext   requestContext,Type controllerType)+438
  System.Web.Mvc.DefaultControllerFactory.CreateController(的RequestContext   requestContext,String controllerName)+226
  System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase   httpContext,IController&amp;控制器,IControllerFactory&amp;厂)   +326 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)+157
  System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,   AsyncCallback回调,对象状态)+88
  System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext的   context,AsyncCallback cb,Object extraData)+50
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   +301 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155

0 个答案:

没有答案