在HttpContext的属性MVC - 线程问题? [英] HttpContext in MVC Attributes - threading issues?

查看:124
本文介绍了在HttpContext的属性MVC - 线程问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的NHibernate会话管理设置类似如下:

I had my NHibernate session management setup like follows:

    protected MvcApplication()
    {
        BeginRequest += delegate
                            {
                                NHibernateSessionManager.Instance.OpenSession();
                             };
        EndRequest += delegate
                            {

                                NHibernateSessionManager.Instance.CloseSession();
                            };
    }

和何时我需要保存到数据库中,我做了一个看起来像这样的一个ActionFilterAttribute:

And for when I needed to save to the database, I made an ActionFilterAttribute that looked like this:

公共类TransactionAttribute:ActionFilterAttribute
    {
        私人ITransaction _currentTransaction;

public class TransactionAttribute: ActionFilterAttribute { private ITransaction _currentTransaction;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _currentTransaction = NHibernateSessionManager.Instance.CurrentSession.Transaction;
        _currentTransaction.Begin();
    }


    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (_currentTransaction.IsActive)
        {
            if (filterContext.Exception == null)
                _currentTransaction.Commit();
            else
            {
                _currentTransaction.Rollback();
            }
        }
        _currentTransaction.Dispose();
    }
}

然后我可能只是[交易]添加到我的操作方法。这似乎在初步测试工作,但我然后我试着用HttpWebRequest的在调用其他应用多次的操作方法,我有问题。使用Fiddler我设置测试POST请求,然后快速连续发射了他们的时间,这表明了以下几点:

and then I could just add [Transaction] to my action method. This seemed to work in initial testing, but I then I tried using at HttpWebRequest to call an action method from another app multiple times and I had issues. Testing with Fiddler I setup a POST request and then fired them off in quick succession and it showed up the following:

红色的是,我认为是线程做各种错误。

THe red ones are various errors that I believe is to do with threading.

我NHibernateSessionManager使用的HttpContext存储这样的会议:

My NHibernateSessionManager uses the HTtpContext to store the session like this:

    public ISession CurrentSession
    {
        get { return (ISession)HttpContext.Current.Items["current.session"]; }
        set { HttpContext.Current.Items["current.session"] = value; }
    }

因此​​,要固定它,我提出我的交易code到我的BeginRequest和EndRequest方法 - 然后我可以连续火了堆。

So, to fixed it, I moved my Transaction code into my BeginRequest and EndRequest methods - and then I could fire off heaps in succession.

我的问题是 - 为什么这个解决?我本来以为我将不得不类似这样:
开始请求 - 打开会话
OnActionExecuting - 开始交易
行动code
OnActionExecuted - 提交交易
结束请求 - 关闭会话

My question is - why did this fix it? I would have thought that I would have had something similar to this: Begin Request - opens session OnActionExecuting - starts transaction action code OnActionExecuted - commits transaction End Request - closes session

和,这将是唯一的每个请求,因此它不应该相互干扰,因为应为每个请求不应该有不同的HttpContext?或者,他们分享什么?

and that this would be unique to each request, so it shouldn't interfere with one another, because there should be a different HttpContext for each request shouldn't there? Or are they shared or something??

有人能告诉我吗?

推荐答案

报价从的 ASP.NET MVC 3 的发行说明:

在ASP.NET MVC的previous版本,
  行动滤膜每创建
  但在少数情况下要求。这个
  行为从来就不是一个保证
  行为而只是一种实现
  细节和过滤器合同
  是考虑他们的无状态的。在
  ASP.NET MVC 3,过滤器会缓存更多
  积极。因此,任何自定义
  行动的过滤器,存放不当
  实例状态可能被打破。

In previous versions of ASP.NET MVC, action filters were created per request except in a few cases. This behavior was never a guaranteed behavior but merely an implementation detail and the contract for filters was to consider them stateless. In ASP.NET MVC 3, filters are cached more aggressively. Therefore, any custom action filters which improperly store instance state might be broken.

这基本上意味着你在你的行动过滤器有 _currentTransaction 实例可能不是你认为它是。所以,要小心/是这个属性注射时=>这是多么不从$ C $清晰C让你表现出来了。

This basically means that the _currentTransaction instance you have in your action filter might not be what you think it is. So be careful how/when is this property injected => it is not clear from the code you have shown.

这篇关于在HttpContext的属性MVC - 线程问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆