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


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

链接地址: http://www.djcxy.com/p/59858.html

上一篇: 如何使用Castle Windsor在MVC中注入UrlHelper

下一篇: 温莎城堡控制器工厂和RenderAction