是什么Session.Abandon()和Session.Clear之间的差值() [英] What is the difference between Session.Abandon() and Session.Clear()

查看:413
本文介绍了是什么Session.Abandon()和Session.Clear之间的差值()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么摧毁一个会话和删除其值之间的差异?能否请您提供一个例子证明这一点?

我搜索了这个问题,但不抓总的答案。有些答案是:


  • Session.Abandon()破坏了会议

  • Session.Clear()只是删除​​所有值

有一个朋友告诉我:


  

清除会话也不会取消
  会议期间,它仍然具有存在
  对于用户,但具有相同ID
  值简单地清除。


  
  

放弃会破坏​​会话
  完全,这意味着你需要
  之前,你就可以开始一个新的会话
  存储会话中的任何更多的价值
  该用户


下面code ++工程,并不会引发任何异常。

  Session.Abandon();
会话[tempKey1] =tempValue1;


  

当你放弃()会话,你(或
  而用户)将获得一个新的
  SESSIONID


当我测试环节中,它不会作出任何改变,当我放弃了会议。

我只是觉得一个区别:
session.Abandon()引发 Session_End中事件


解决方案

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.clear.aspx\">Clear - 移除会话状态集合中的所有键和值

放弃 - 删除存储在一个会话的所有对象。如果不显式调用Abandon方法,服务器当会话超时删除这些对象,并破坏了会话。结果
这也引起了像<一个事件href=\"http://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstatemodule.end.aspx\">Session_End.

Session.Clear可以相比的从货架移除所有的书籍的,而Session.Abandon更像的扔掉整个大陆架

您说的:


  

当我测试环节中,它不会作出任何改变,当我放弃了会议。


此,而你是一个请求仅中做的是正确的。结果
在接下来的请求的会话会有所不同。但会话ID可以被重用,以便该ID将保持不变。

如果您将使用Session.Clear你将不得不在许多要求同一个会话。

一般情况下,在大多数情况下,你需要使用Session.Clear。结果
您可以使用Session.Abandon如果您确定用户是要离开你的网站。

于是回差异:


  1. 放弃加薪要求Session_End中。

  2. 清除immidiately删除的项目,放弃不。

  3. 放弃释放将sessionState对象及其项目,因此它可以收集来释放资源BA垃圾。清除保持SessionState会和与之相关的资源。

What is the difference between destroying a session and removing its values? Can you please provide an example demonstrating this?

I searched for this question, but don't grasp total answer. Some answers are:

  • Session.Abandon() destroys the session
  • Session.Clear() just removes all values

A friend told me this:

Clearing the session will not unset the session, it still exists with the same ID for the user but with the values simply cleared.

Abandon will destroy the session completely, meaning that you need to begin a new session before you can store any more values in the session for that user.

The below code works and doesn't throw any exceptions.

Session.Abandon();
Session["tempKey1"] = "tempValue1";

When you Abandon() a Session, you (or rather the user) will get a new SessionId

When I test Session, it doesn't makes any change when I Abandon the session.

I just find one difference: session.Abandon() raises Session_End event

解决方案

Clear - Removes all keys and values from the session-state collection.

Abandon - removes all the objects stored in a Session. If you do not call the Abandon method explicitly, the server removes these objects and destroys the session when the session times out.
It also raises events like Session_End.

Session.Clear can be compared to removing all books from the shelf, while Session.Abandon is more like throwing away the whole shelf.

You say:

When I test Session, it doesn't makes any change when I Abandon the session.

This is correct while you are doing it within one request only.
On the next request the session will be different. But the session ID can be reused so that the id will remain the same.

If you will use Session.Clear you will have the same session in many requests.

Generally, in most cases you need to use Session.Clear.
You can use Session.Abandon if you are sure the user is going to leave your site.

So back to the differences:

  1. Abandon raises Session_End request.
  2. Clear removes items immidiately, Abandon does not.
  3. Abandon releases the SessionState object and its items so it can ba garbage collected to free the resources. Clear keeps SessionState and resources associated with it.

这篇关于是什么Session.Abandon()和Session.Clear之间的差值()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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