Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • kapil 2 posts 72 karma points
    1 week ago
    kapil
    0

    Failed to create an instance of controller type HomeController (see inner exception)

    Hi,

    I am implementing Autofac for MVC controller, inheriting from RenderMVCController. But I am getting below error. Help on this is greatly appreciated.

    ======Controller : ======

    public class HomeController : RenderMvcController
    {
        private readonly IStateRepository _stateRepository;
        public HomeController(StateRepository stateRepository)
        {
           _stateRepository = stateRepository;
        }
        public override ActionResult Index(ContentModel model)
        {
            HomeViewModel homeModel = SetupDataView(model);
            return View(Constants.HomeView, homeModel);
        }
     }
    

    AutoFac Code:

        private static void ConfigureAutoFac(Composition composition)
    
        {
            var stateRository = new StateRepository();
            var builder = new ContainerBuilder();
            builder.RegisterInstance(new StateRepository()).As<IStateRepository>();
            builder.RegisterType<HomeController>();
    
      builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
    
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    
        }
    

    ======Error:======

    [Exception: Failed to create an instance of controller type MyApplication.HomeController (see inner exception).] Umbraco.Web.Mvc.ContainerControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +141 Umbraco.Web.Mvc.UmbracoControllerFactory.CreateController(RequestContext requestContext, String controllerName) +93 Umbraco.Web.Mvc.RenderControllerFactory.CreateController(RequestContext requestContext, String controllerName) +14 Umbraco.Web.Mvc.MasterControllerFactory.CreateController(RequestContext requestContext, String controllerName) +53 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() +443 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +132 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

  • Paul Johnson 2 posts 72 karma points
    1 week ago
    Paul Johnson
    0

    Constructor is asking for concrete type, request the interface instead

  • kapil 2 posts 72 karma points
    7 days ago
    kapil
    0

    Thanks for your reply Paul. I changed as you suggested, still same issue:

      public class HomeController : RenderMvcController
        {
            private readonly IStateRepository _stateRepository;
            public HomeController(IStateRepository stateRepository)
            {
               _stateRepository = stateRepository;
            }
        }
    
  • Dave Woestenborghs 3159 posts 10354 karma points MVP 3x admin c-trib
    6 days ago
    Dave Woestenborghs
    0

    Hi kapil,

    Can you remove this line from your autofac config :

     builder.RegisterType<HomeController>();
    

    Your controller are normally picked up by the line after that

    builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
    

    But this requires the controller to be in the same assembly as the autofac registration

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft