控制器“TestController”的单个实例;不能用于处理多个请求

发布于 2024-12-05 20:00:36 字数 3502 浏览 6 评论 0原文

我对统一的生命周期管理器有一些问题,它像单例一样使用对象,但在配置中我将其设置为“PerWebRequest”。

错误是: 控制器“TestController”的单个实例不能用于处理多个请求。如果正在使用自定义控制器工厂,请确保它为每个请求创建一个新的控制器实例。

PerWebRequest 代码:

public class UnityPerWebRequestLifetimeManager : LifetimeManager
{
    private HttpContextBase _httpContext;

    public UnityPerWebRequestLifetimeManager()
        : this(new HttpContextWrapper(HttpContext.Current))
    {
    }

    public UnityPerWebRequestLifetimeManager(HttpContextBase httpContext)
    {
        _httpContext = httpContext;
    }

    private IDictionary<UnityPerWebRequestLifetimeManager, object> BackingStore
    {
        get
        {
            _httpContext = (HttpContext.Current != null) ? new HttpContextWrapper(HttpContext.Current) : _httpContext;

            return UnityPerWebRequestLifetimeModule.GetInstances(_httpContext);
        }
    }

    private object Value
    {
        [DebuggerStepThrough]
        get
        {
            IDictionary<UnityPerWebRequestLifetimeManager, object> backingStore = BackingStore;

            return backingStore.ContainsKey(this) ? backingStore[this] : null;
        }

        [DebuggerStepThrough]
        set
        {
            IDictionary<UnityPerWebRequestLifetimeManager, object> backingStore = BackingStore;

            if (backingStore.ContainsKey(this))
            {
                object oldValue = backingStore[this];

                if (!ReferenceEquals(value, oldValue))
                {
                    IDisposable disposable = oldValue as IDisposable;

                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }

                    if (value == null)
                    {
                        backingStore.Remove(this);
                    }
                    else
                    {
                        backingStore[this] = value;
                    }
                }
            }
            else
            {
                if (value != null)
                {
                    backingStore.Add(this, value);
                }
            }
        }
    }

    [DebuggerStepThrough]
    public override object GetValue()
    {
        return Value;
    }

    [DebuggerStepThrough]
    public override void SetValue(object newValue)
    {
        Value = newValue;
    }

    [DebuggerStepThrough]
    public override void RemoveValue()
    {
        Value = null;
    }
}

控制器:

public class TestController : Controller
{
    //
    // GET: /Test/

    public TestController()
    {

    }

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult RadioButtonList()
    {
        return View("TestControl");
    }
}

控制器工厂:

public class ControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        return (controllerType == null) ? base.GetControllerInstance(requestContext, controllerType) : IoC.Resolve<IController>(controllerType);
    }
}

我尝试像这样使用它:

……

<% Html.RenderAction<TestController>(c => c.RadioButtonList()); %>

                        <% Html.RenderAction<TestController>(c => c.RadioButtonList()); %>

在其中一个视图中,

我不知道这里出了什么问题?

谢谢。

I have Some issues with the life time manager in unity, it uses the object like its singleton, but in the configuration I set it to "PerWebRequest".

The Error is:
A single instance of controller 'TestController' 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.

The PerWebRequest code:

public class UnityPerWebRequestLifetimeManager : LifetimeManager
{
    private HttpContextBase _httpContext;

    public UnityPerWebRequestLifetimeManager()
        : this(new HttpContextWrapper(HttpContext.Current))
    {
    }

    public UnityPerWebRequestLifetimeManager(HttpContextBase httpContext)
    {
        _httpContext = httpContext;
    }

    private IDictionary<UnityPerWebRequestLifetimeManager, object> BackingStore
    {
        get
        {
            _httpContext = (HttpContext.Current != null) ? new HttpContextWrapper(HttpContext.Current) : _httpContext;

            return UnityPerWebRequestLifetimeModule.GetInstances(_httpContext);
        }
    }

    private object Value
    {
        [DebuggerStepThrough]
        get
        {
            IDictionary<UnityPerWebRequestLifetimeManager, object> backingStore = BackingStore;

            return backingStore.ContainsKey(this) ? backingStore[this] : null;
        }

        [DebuggerStepThrough]
        set
        {
            IDictionary<UnityPerWebRequestLifetimeManager, object> backingStore = BackingStore;

            if (backingStore.ContainsKey(this))
            {
                object oldValue = backingStore[this];

                if (!ReferenceEquals(value, oldValue))
                {
                    IDisposable disposable = oldValue as IDisposable;

                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }

                    if (value == null)
                    {
                        backingStore.Remove(this);
                    }
                    else
                    {
                        backingStore[this] = value;
                    }
                }
            }
            else
            {
                if (value != null)
                {
                    backingStore.Add(this, value);
                }
            }
        }
    }

    [DebuggerStepThrough]
    public override object GetValue()
    {
        return Value;
    }

    [DebuggerStepThrough]
    public override void SetValue(object newValue)
    {
        Value = newValue;
    }

    [DebuggerStepThrough]
    public override void RemoveValue()
    {
        Value = null;
    }
}

The controller:

public class TestController : Controller
{
    //
    // GET: /Test/

    public TestController()
    {

    }

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult RadioButtonList()
    {
        return View("TestControl");
    }
}

The Controller Factory:

public class ControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        return (controllerType == null) ? base.GetControllerInstance(requestContext, controllerType) : IoC.Resolve<IController>(controllerType);
    }
}

And in one of the views I am trying to use it like this:

...

<% Html.RenderAction<TestController>(c => c.RadioButtonList()); %>

                        <% Html.RenderAction<TestController>(c => c.RadioButtonList()); %>

...

I don't know what wrong here?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2024-12-12 20:00:37

两个统一控制器请求都是在同一个 HTTP 请求/回复中创建的,因此您获得相同的实例。您需要进行切换,以便控制器具有瞬态生命周期。

我会切换到 DependencyResolver 而不是使用 ControllerFactory,因为您正在运行 MVC3。

Both unity controller requests are created within the same HTTP request/reply, hence you get the same instance. You need to switch so that the controllers have a Transient lifetime.

I would switch to DependencyResolver instead of using ControllerFactory since you are running MVC3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文