views:

256

answers:

1

I am running into an issue when using my Castle Windsor Controller Factory with the new RenderAction method. I get the following error message:

A single instance of controller 'MyController' cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request.

This is the code in my controller factory:

public class CastleWindsorControllerFactory : DefaultControllerFactory
    {
        private IWindsorContainer container;

        public CastleWindsorControllerFactory(IWindsorContainer container)
        {
            this.container = container;
        }

        public override IController CreateController(RequestContext requestContext, string controllerName)
        {
            return container.Resolve(controllerName) as IController;
        }

        public override void ReleaseController(IController controller)
        {
            this.container.Release(controller);
        }
    }

Does anyone know what changes I need to make to make it work with RenderAction?

I also find the error message slightly strange because it talks about multiple requests, but from what I can tell RenderAction doesn't actually create another request (BeginRequest isn't fired again).

+3  A: 

I believe the default config for Castle Windsor is a Singleton. You need to change this to Transient in your Web.Config or by putting this attribute on your class [Transient].

Nissan Fan
You are correct. The default lifestyle for Windsor components is singleton.
Patrick Steele
You can also register it as transient via fluent API http://stw.castleproject.org/Windsor.Registering-components-one-by-one.ashx?HL=transient#Configuring_components_lifestyle_3 . Using XML for registration is not regarded as best practice.
Krzysztof Koźmic

related questions