Search code examples
sitecoresitecore7.2glass-mapper

No component for supporting the service Sitecore.Mvc.Controllers.SitecoreController was found - Sitecore 7.2


Trying to Register the dependency that need to be passed to the Controller Methods as an Interface and after doing some research the below needs to done but after setting it Sitecore throws this errpr

ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container.Kernel));
container.Register(Classes.FromThisAssembly().BasedOn<IController>().LifestyleTransient().Configure( x => x.Named(x.Implementation.FullName)));

Code Snippet in the controller is

public ActionResult Footer(ISomeFactory someFactory) {}

I am using Glass Mapeer and Castle Windsor for IOC.


Solution

  • You need to tell Castle how to resolve SitecoreController which is not defined in your assembly. Try this:

            container.Register(
                Classes.FromThisAssembly()
                    .BasedOn<IController>()
                    .LifestyleTransient()
                    .Configure(x => x.Named(x.Implementation.FullName)),
               Component.For<SitecoreController>()
                        .ImplementedBy<SitecoreController>()
                        .LifestylePerWebRequest()
                        .DependsOn(new {databaseName = Sitecore.Context.Database})
                );
    

    EDIT: Based on comment you can expand this registration to something like this. You may need to review Lifestyle settings for each object, and depending on your Controller's constructors you may need to add in additional implementations.

      container.Register(
                Classes.FromThisAssembly().BasedOn<IController>().LifestyleTransient(),
                Component.For<ISitecoreContext>().ImplementedBy<SitecoreContext>().LifestylePerWebRequest(),
                Component.For<ISitecoreService>().ImplementedBy<SitecoreService>(),
                Component.For<IGlassHtml>().ImplementedBy<GlassHtml>().LifestylePerWebRequest(),
                Component.For<SitecoreController>()
                    .ImplementedBy<SitecoreController>()
                    .LifestylePerWebRequest()
                    .DependsOn(new {databaseName = Sitecore.Context.Database})
               );