1

I have just started using Windsor to resolve dependency. But i am getting below error while passing string value in my Controller.I checked couple of similar Windsor Post but no luck.

public class HomeController : Controller
{
    public readonly String _A = string.Empty;
    public HomeController(string A)
    {
        this._A = A;        
    }
}

Installer

public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Classes.FromThisAssembly()
            .BasedOn<IController>()
            .LifestyleTransient());
    } 

WindsorControllerFactory

public class WindsorControllerFactory : DefaultControllerFactory
    {
        private readonly IKernel kernel;

        public WindsorControllerFactory(IKernel kernel)
        {
            this.kernel = kernel;
        }

        public override void ReleaseController(IController controller)
        {
            kernel.ReleaseComponent(controller);
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
            }
            return (IController)kernel.Resolve(controllerType);
        }
    }

Do i need to wrap 'A' inside a class and resolve it's respective Interface ?

Dev
  • 295
  • 1
  • 4
  • 22

2 Answers2

2

Yes that would be the preferable solution as it makes the dependency very clear.

public class HomeControllerConfig
{
    public string A { get; set; }
}

public class HomeController : Controller
{
    public readonly String _A = string.Empty;
    public HomeController(HomeControllerConfig config)
    {
        this._A = config.A;        
    }
}

Installer

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    ...
    container.Register(Component.For<HomeControllerConfig>().Instance(new HomeControllerConfig { A = "..." }));
} 
Phil Degenhardt
  • 7,215
  • 3
  • 35
  • 46
0

It depends on what A really is, semantically for your application, but in short, no - you don't have to wrap it. The documentation explains in detail how to configure it.

Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115