views:

37

answers:

1

Hi,

This is how my Application_Start looks:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    _container.Register(Component.For<IWindsorContainer>()
                            .Instance(_container),
                        Component.For<IView, IViewPageActivator>()
                            .ImplementedBy<RazorView>(),
                        Component.For<IFilterProvider>()
                            .ImplementedBy<WindsorFilterAttributeFilterProvider>(),
                        Component.For<IControllerFactory>()
                            .ImplementedBy<WindsorControllerFactory>(),
                        Component.For<ControllerContext>()
                            .ImplementedBy<ControllerContext>()
        );


    _container.Register(
        AllTypes.Of<IController>()
            .FromAssembly(Assembly.GetExecutingAssembly())
            .Configure(c => c.LifeStyle.Transient)
        );

Yet when trying to run the solution I get the following error:

Can't create component 'System.Web.Mvc.RazorView' as it has dependencies to be satisfied. 
System.Web.Mvc.RazorView is waiting for the following dependencies: 

Keys (components with specific keys)
- viewPath which was not registered. 
- layoutPath which was not registered. 
- runViewStartPages which was not registered. 
- viewStartFileExtensions which was not registered. 

How do I setup the container such that it can get the required information dynamically at run time? As I assume at the very least viewPath will change for each Controller.

+2  A: 

I'm still playing around with MVC, but I think I can point you in the right direction.

When you registered the component for RazorView you want to use the DynamicParamters method - eg:

Component.For<IView>().ImplementedBy<RazorView>()
                .DynamicParameters((kernel, dict) => {
                    dict["viewPath"] = "~";
                    dict["layoutPath"] = "~";
                    dict["runViewStartPages"] = true;
                    dict["viewStartFileExtensions"] = new List<string>() { "cshtml"};
                })

I also didn't have Windsor cast RazorView as an IViewPageActivator since it does not implement that interface. If you read The post from Brad Wilson your implementation of IDependencyResolver should return null for the IViewPageActivator if you don't have one.

Hope that helps.

Sugendran
Yes this works. I am also able to get the page to render if I leave out the IView and ControllerContext (and IViewPresenter as mentioned by Brad). So is it even necessary to put an implementation of a IView in the container?
NetRunner
hey netrunner, you should mark the answer up!
andy