完全替换ASP.Net会话 [英] Replacing ASP.Net's session entirely

查看:286
本文介绍了完全替换ASP.Net会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.Net会议似乎完美的一个传统的WebForms应用程序,但他们做一些事情,是一个现代的AJAX和MVC应用程序的严重问题。

ASP.Net session appear perfect for a traditional WebForms app, but they do some things that are a serious problem for a modern AJAX and MVC application.

具体地,只有3访问ASP.Net提供商的方法:

Specifically there are only 3 ways to access the ASP.Net provider:


  1. 锁定读取和放大器;写(默认) - 会话从的AcquireRequestState 射击锁定,直到 ReleaseRequestState 火灾。如果3请求从浏览器中出现一次,他们会在服务器上排队。这是MVC 2的唯一选择,但MVC 3让...

  1. Locking read & write (default) - the session is locked from AcquireRequestState firing until ReleaseRequestState fires. If 3 requests occur from the browser at once they'll queue up on the server. This is the only option in MVC 2, but MVC 3 allows...

非锁定只读 - 会话没有锁定,但不能保存到。这似乎是不可靠,虽然,因为一些读似乎再次锁定该会话。

Non-locking read only - the session isn't locked, but can't be saved to. This appears to be unreliable though, as some reads appear to lock the session again.

会话禁用 - 任何试图读取或写入会话抛出一个异常

Session disabled - any attempt to read or write to the session throws an exception.

然而,随着现代MVC应用程序我有很多AJAX事件同时发生的 - 我不希望他们在服务器上的队​​列中,但我也希望他们能够写入会话

However with a modern MVC app I have lots of AJAX events happening at once - I don't want them the queue on the server but I do want them to be able to write to the session.

我要的是第四种模式:脏读,最后一次写入胜

What I want is a fourth mode: Dirty read, last write wins

我觉得(高兴能校正),要做到这一点的唯一方法是完全替代ASP.Net的会议。我可以写我的自己提供商,但ASP仍然会与3 patters之一叫它它支持。有没有什么办法让ASP.Net支持乐观并发?

I think (happy to be corrected) that the only way to do this is to completely replace ASP.Net's sessions. I can write my own provider, but ASP will still call it with one of the 3 patters it supports. Is there any way to make ASP.Net support optimistic concurrency?

这让我代替所有呼叫一个新的类,基本上做同样的事情了会议,但没有锁定 - 这是一种痛苦。

This leaves me replacing all calls to the session with a new class that basically does the same thing, but doesn't lock - which is a pain.

我想保持尽可能多的当前会话的东西,我可以(最显著的各种日志会话ID)与code更换的最低金额。有没有办法做到这一点?理想的情况是我愿意 HttpContext.Current.Session 来点我​​的新类,但没有ASP.Net锁定任何请求。

I want to keep as much of the current session stuff as I can (most significantly session IDs in various logs) with the minimum amount of code replacement. Is there any way to do this? Ideally I'd want HttpContext.Current.Session to point to my new class but without ASP.Net locking any requests.

有没有人已经做了这样的事情?这似乎很奇怪,与所有的AJAXey MVC应用程式在那里,这是用ASP的一个新问题。

Has anyone already done something like this? It seems odd that with all the AJAXey MVC apps out there this is a new problem with ASP.

推荐答案

首先我说MS asp.net在<强某处有锁的核心是asp.net处理页面会议, > webengine4.dll DLL的asp.net 4

First of all I say that MS asp.net have lock in the core of "asp.net processing a page" the session, somewhere in the "webengine4.dll" dll for asp.net 4

和我说,从MS的角度 asp.net行为正确,并锁定会话这种方式,因为asp.net不知道我们保持什么类型的信息会话这样可以使一个正确的最后写入胜。

And I say that from the view point of MS asp.net act correct and lock the session this way because asp.net can not know what type of information we keep on session so can make a correct "last write wins".

同时正确的是锁作为会议的整个页面,因为这种方式是给你的程序的一个非常重要的同步,从现在的经验,我说你需要它对于大多数你的行动。之后,我有我的更换毫秒会议上,我所有的行动,我需要全局锁,要不然我有双重刀片,双行动的问题等。

Also correct is lock for the session the full page because that way is gives your a very important synchronizations of your program, that from experience now I say you need it for most of your actions. After I have replace the ms session with mine, on all of my actions I need to make global lock, or else I have problems with double inserts, double actions, etc.

我给为例,我认识这里的问题。会话数据被保存在SessionSateItemCollection即键的列表。当会话读或写这个集合是做所有的人在一起。因此,让我们看到这个情况。

I give an example of the issue that I recognize here. Session data are saved on SessionSateItemCollection that is a list of keys. When session reads or write this collection is do it all of them together. So let see this case.

我们对会议的树变量VAR1,VAR2,VAR3

we have tree variables on session, "VAR1", "VAR2", "VAR3"

1 结果
  的getSession(实际上得到的所有数据,并将其放置在列表)结果
  会议[VAR1] =DATA1;结果
  savesession()结果
结果是VAR1 =数据1,VAR2 =(VAR2的最后的数据),VAR3 =(VAR3的最后的数据)

Page 1.
getsession (actually get all data and place them on list)
session[VAR1] = "data1";
savesession()
result is VAR1=data1, VAR2=(last data of var2), VAR3=(last data of var3)

第2页。结果
  的getSession(实际上得到的所有数据,并将其放置在列表)结果
  会议[VAR2] =数据2;结果
  savesession()结果
结果是VAR1 =(VAR1的最后的数据),VAR2 = DATA2,VAR3 =(VAR3的最后的数据)

Page 2.
getsession (actually get all data and place them on list)
session[VAR2] = "data2";
savesession()
result is VAR1=(last data of var1), VAR2=data2, VAR3=(last data of var3)

第3页。结果
  的getSession(实际上得到的所有数据,并将其放置在列表)结果
  会议[VAR3] =数据3;结果
  savesession()结果
结果是VAR1 =(VAR1的最后的数据),VAR2 =(VAR2的最后的数据)VAR3 =数据3

Page 3.
getsession (actually get all data and place them on list)
session[VAR3] = "data3";
savesession()
result is VAR1=(last data of var1), VAR2=(last data of var2) VAR3="data3"

请注意这里每个页面有数据的克隆,因为他从会议中读取它们(例如:由于他们与从数据库readed)

Note here that each page have a clone of the data, as he read them from the session medium (eg as they last readed from database)

如果我们让这3页与出锁定跑,我们没有真正脏读,幽冥最后一次写入胜 - 我们在这里是最后一次写入破坏他人,因为如果你再想想吧,当VAR1变化,为什么VAR2和VAR3保持不变?如果你有改变VAR2别处。

If we let this 3 pages run with out locking, we do not have actually dirty read, nether "last write wins" - What we have here is "the last write destroy the others", because if you think it again, when VAR1 change, why the VAR2 and VAR3 stay the same ? what if you have change VAR2 somewhere else.

和出于这个原因,我们不能让这种与失锁,因为它是。

and for that reason we can not let this with out lock as it is.

现在成像与20个变量......完全一塌糊涂。

And now imaging that with 20 variables... totally mess.

由于许多池asp.net的多线程,以保持整个池相同的数据,并在电脑的唯一方法是有一个共同的数据库,或者一个共同的象asp.net状态服务的所有处理程序。

Because of the multithread of asp.net of many pools, the only way to keep the same data across pools, and across computers is to have a common database, or a common to all process program like the asp.net State Service.

我选择有一个共同的数据库更容易比创建一个程序,该提议。

I select to have a common database as more easy than create a program for that propose.

现在,如果我们连接的一个cookie 我们做,以用户会话的数据用户,我们使用一个完全自定义的数据库字段控制数据我们知道我们喜欢锁,没有什么,如何保存或改变,或比较和保留最后的写胜数据,我们可以使这项工作,而且完全禁用asp.net会话是所有做了一个通用会话门将的需求,但没有作出处理特殊情况是这样的。

Now if we connect a cookie that we make, to the user to the data user of the session, and we control the data using a totally custom database field that we know what we like to lock, what not, how to save or change, or compare and keep the last write wins data, we can make this work, and totally disable the asp.net session that is a generic session keeper made for all needs, but not made to handle special cases like this one.

可以使ASP.NET在未来的版本这项工作,是的,他可以​​通过被称为肮脏的额外字段,并在会议上保存数据,让使用肮脏的领域,或者其它类似的数据合并,但可没有把这个现在的工作,因为它是 - 至少从我发现到现在结果
 其实SessionStateItem对性能的脏标志,但没有做出保存数据的月底合并。我真的很喜欢,如果另外一个人想了一个解决方案,以现有的MS asp.net会话状态的门将,我写什么我现在已经找到了,但是这并不意味着这是肯定没有办法 - 我没有找到它,因为它是。

Can asp.net make this work on the future release, yes he can by making an extra field called dirty, and on the session save data, make a merge of data using the dirty field, or something like that, but can not make this now work as it is - at least from what I have found until now.
Actually SessionStateItem have a dirty flag on properties, but did not make this merge on the end of save data. I will really love if some one else think for a solution to the existing ms asp.net session state keeper, I am write what I have found up to now, but this is not mean that for sure there is no way - I have not found it as it is.

希望所有的帮助:)

在这个答案 http://stackoverflow.com/a/3660837/159270 詹姆斯写一个自定义模块之后说:我仍然无法相信ASP.Net会话的自定义实现锁定整个请求的会话

in this answer http://stackoverflow.com/a/3660837/159270 the James after write a custom module says: I still can't believe the custom implementation of ASP.Net Session locks the session for the whole request.

这篇关于完全替换ASP.Net会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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