Copied to clipboard

Flag this post as spam?

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


  • nickornotto 397 posts 900 karma points
    Dec 17, 2020 @ 11:38
    nickornotto
    0

    How to register Umbraco RenderMvcControllers in glabal asax

    I am trying to use RenderMvcController and SurfaceController with dependency injection but it doesn't work.

    public class SupplierRenderMvcController : RenderMvcController
    {
        public HomeRenderMvcController(IMembershipService membershipService)
        {
            _membershipService = membershipService;
        }
    }
    
    public class SupplierController : SurfaceController
    {
        public SupplierController(IMembershipService membershipService)
        {
            _membershipService = membershipService;
        }
    }
    

    It is saying:

    No parameterless constructor defined for this object.

    Umbraco's controllers are registered in in Global.asax:

            builder.RegisterControllers(typeof(UmbracoApplication).Assembly);
            builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
    

    I have api controllers using IoC with no problem in other projects of the same solution.

    The above controlers are in the main project of the solution though.

    This is my full Global.asax of the main project:

    public class MvcApplication : IApplicationEventHandler
    {
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            AreaRegistration.RegisterAllAreas();
            BundleConfig.RegisterBundles(BundleTable.Bundles);
    
            GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            RegisterIoCContainer();
        }
    
        public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        private void RegisterIoCContainer()
        {
            var builder = new ContainerBuilder();
    
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinderProvider();
    
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    
            builder.RegisterControllers(typeof(UmbracoApplication).Assembly);
            builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
    
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly‌​());
    
            builder.RegisterModule<WebApiConfig>();
    
            var container = builder.Build();
    
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
        }
    }
    

    MembershipService is registered within WebApiConfig:

    public class WebApiConfig : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.AddMediatR(this.GetType().Assembly);
    
            builder.RegisterType<MembershipService1>().As<IMembershipService>();
        }
    }
    
  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Jan 04, 2021 @ 12:58
    Huw Reddick
    0

    is the error referring to this

    public class SupplierRenderMvcController : RenderMvcController
    {
        public HomeRenderMvcController(IMembershipService membershipService)
        {
            _membershipService = membershipService;
        }
    }
    

    Shouldn't the constructor be called SupplierRenderMvcController and not HomeRenderMvcController

  • nickornotto 397 posts 900 karma points
    Mar 24, 2022 @ 17:44
    nickornotto
    0

    All my Api controller (inheriting from UmbracoApiController) fail with the same error. They started failing a while ago already and currently I am unable to debug anything i my localhost.

    I have to copy dlls into the test and there it fails or not randomly. If it fails I copy more dlls (which have not been changed on the current run) and the site normally picks up.

    Same on public site.

    But on local it never works.

    I fixed my code in the question, the constructor is SupplierRenderMvcController indeed not HomeRenderMvcController but it's still not working.

Please Sign in or register to post replies

Write your reply to:

Draft