Search code examples
dependency-injectionsitecorecastle-windsorsitecore-mvcsitecore8.1

Castle Windser implementation issue in Sitecore 8.1 MVC project


I am going to implement castle windsor DI in Sitecore 8.1 MVC project.At website level I have created interface,class and repository as well.I have gone through various article but didn't found straight forward approach. Controller part is as per following:

public class CommonController : Controller
{
    private readonly ICommon _service;
    public CommonController(ICommon commonService)
    {
        this._service = commonService;
    }
    public ActionResult GetProductDetail()
    {
        var CommonModel = _service.GetProductDetail();
        return View(CommonModel);
    }
}

I got following error when I browse:

Server Error in '/' Application.

No parameterless constructor defined for this object. 

I know still need to implement DI part.

I have gone through the solution following article: Using Castle Windsor with Sitecore MVC for Dependency Injection

but now got following error:

The given key was not present in the dictionary. Description: An unhandled exception occurred.

Any suggestion?


Solution

  • The article tells you how to configure windsor. But what it doesn't tell you is that you have to configure your container.

    For windsor to be able to resolve your ICommon commonService you need to tell it how it has to do so.

    There are several ways to do so. Below is just one example of how I've got it working in one project.

    Hope it helps you.

    Hans

    public class WebInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(Component.For(typeof(ICommon)).ImplementedBy(typeof(CommonService)).LifestyleTransient());
    
        }
    }
    
    public static class DependencyInjection
    {
        private static IWindsorContainer container;
        private static readonly object padlock = new object();
    
        public static IWindsorContainer Container
        {
            get
            {
                lock (padlock)
                {
                    if (container == null)
                    {
                        InitializeDependencyInjection();
                    }
    
                    return container;
                }
            }
        }
    
        private static void InitializeDependencyInjection()
        {
            container = new WindsorContainer();
            container.AddFacility<FactorySupportFacility>();
            container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));
    
            container.Install(new Core.Installers.WebInstaller());
    
            // Wiring up DI for the SOLR integration
            //new WindsorSolrStartUp(container).Initialize();
        }
    
        /// <summary>
        ///     http://www.superstarcoders.com/blogs/posts/using-castle-windsor-with-sitecore-mvc-for-dependency-injection.aspx
        /// </summary>
        public static void SetupControllerFactory()
        {
            Container.Install(FromAssembly.This());
    
            // Transient needed for sitecore running multiple instances of a controller for controller renderings
            Container.Register(Classes.FromThisAssembly().BasedOn<IController>().LifestyleTransient());
    
            IControllerFactory controllerFactory = new WindsorControllerFactory(Container);
    
            var scapiSitecoreControllerFactory = new SitecoreControllerFactory(controllerFactory);
    
            ControllerBuilder.Current.SetControllerFactory(scapiSitecoreControllerFactory);
        }
    }
    
    public class InitializeWindsorControllerFactory
    {
        public virtual void Process(PipelineArgs args)
        {
            DependencyInjection.SetupControllerFactory();
        }
    }