public class AutofacComponent : IComponent
{
public void Initialize()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(UmbracoApplication).Assembly);
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
ApplicationComposer.cs
public class ApplicationComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Components().Append<AutofacComponent>();
}
}
SiteLayoutController.cs
public class SiteLayoutController : SurfaceController
{
private INavigation _navigation;
public SiteLayoutController(INavigation navigation)
{
_navigation = navigation;
}
public ActionResult RenderMainNavigation()
{
var root = Umbraco.ContentAtRoot().First();
return PartialView("Layout/_Navigation", _navigation.GetItems(root, false));
}
}
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 : ======
AutoFac Code:
======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)
Constructor is asking for concrete type, request the interface instead
Thanks for your reply Paul. I changed as you suggested, still same issue:
Hi kapil,
Can you remove this line from your autofac config :
Your controller are normally picked up by the line after that
But this requires the controller to be in the same assembly as the autofac registration
Dave
Hi, I'm having the same issue.
AutofacComponent.cs
ApplicationComposer.cs
SiteLayoutController.cs
then presumably no concrete classes that implement INavigation are registered in the ioc container.
Hi Paul, I've also tried registering INavigation as below but still the same error.
Does Navigation have any injected dependencies when it is instantiated? Are they all registered?
Hi Paul, my Navigation class got no constructor.
is working on a reply...