Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Castle Windsor Controller Factory and RenderAction

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).

like image 965
Bradley Landis Avatar asked Dec 29 '22 17:12

Bradley Landis


1 Answers

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].

like image 59
Keith Adler Avatar answered Dec 31 '22 06:12

Keith Adler