" HttpContext.Current.Session" VS的Global.asax" this.Session" [英] "HttpContext.Current.Session" vs Global.asax "this.Session"

查看:147
本文介绍了" HttpContext.Current.Session" VS的Global.asax" this.Session"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

近日,而在一些code在工作中ASP.NET项目。我们需要跟踪UTIL采取用户活动的基本指标(网页点击数等),我们将跟踪他们的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.aspx\"><$c$c>Session,然后通过 Session_End中将数据保存到数据库中 的Global.asax

我开始劈砍而去,初始code正常工作,更新数据库上的每个页面加载。我想删除此DB打在每个请求,但并不仅仅是靠 Session_End中来存储所有数据。

所有跟踪code被包裹在跟踪类,包括基本上包裹Session变量属性。

问题的是,当我执行 Tracker.Log() Session_End中法,<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpcontext.current.aspx\"><$c$c>HttpContext.Current.Session在跟踪code用<一个失败href=\"http://msdn.microsoft.com/en-us/library/system.nullreferenceexception.aspx\"><$c$c>NullReferenceException.现在,这是有道理的,因为<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpcontext.aspx\"><$c$c>HttpContext始终与电流请求,当然在 Session_End中的,有没有要求。

我知道的Global.asax 具有返回会话属性 HttpSessionState ,实际上似乎做工精细(我结束了在以跟踪其注入)。

但是我很好奇,到底如何才能得到相同的参考由的Global.asax HttpSessionState 对象code>来自的之外的Global.asax 的?

在此先感谢你们,我AP preciate输入。 :)


解决方案

Global.asax中实现HttpApplication的 - 这就是你说的,当你打电话的这个在IT

有关的HttpApplication MSDN文档提供了有关如何可以弄个细节它在例如一个HttpHandler,然后得到它获得的各种属性。

您的应用程序可以创建一个HttpApplication的多个实例来处理并行请求,而这些实例可以被重新利用,所以才拿起它在某种程度上是不会机制保障,你有正确的。

我也还想补充一个值得注意的问题 - 如果你的应用程序崩溃,有没有机制保障了Session_End中将会被调用,你会失去所有的数据在所有会议,显然不是一件好事

我同意,记录每一页上可能不是一个好主意,但也许中途之家与一些异步日志记录发生的一切 - 你火细节开了一个日志类,每一个现在,然后登录你所追求的细节 - 仍然不是100%固体如果应用程序崩溃,但你不太可能失去一切。

Recently, while working on some code for an ASP.NET project at work. We needed a tracking util to take basic metrics on user activity (page hit count etc) we would track them in Session, then save the data to DB via Session_End in Global.asax.

I began hacking away, the initial code worked fine, updating the DB on each page load. I wanted to remove this DB hit on each request though and just rely on Session_End to store all the data.

All of the tracking code is encapsulated in the Tracker class, including properties that essentially wrap the Session variables.

The problem is that when I executed Tracker.Log() in the Session_End method, the HttpContext.Current.Session in the Tracker code was failing with a NullReferenceException. Now, this makes sense since HttpContext always relates to the current request, and of course in Session_End, there is no request.

I know that Global.asax has a Session property which returns a HttpSessionState that actually seems to work fine (I ended up injecting it in to the tracker)..

But I am curious, how the hell can I get the same reference to the HttpSessionState object used by Global.asax from outside of Global.asax?

Thanks in advance guys, I appreciate the input. :)

解决方案

Global.asax implements HttpApplication - which is what you are talking to when you call this from within it.

The MSDN documentation for HttpApplication has details on how you can get hold of it in an HttpHandler for example, and then get access to the various properties on it.

HOWEVER

Your application can create multiple instances of HttpApplication to handle parallel requests, and these instances can be re-used, so just picking it up somehow isn't going to guarentee that you have the right one.

I too would also add a note of caution - if your application crashes, there's no guarentee that session_end is going to be called, and you'll have lost all the data across all sessions, clearly not a good thing.

I agree that logging on every page is probably not a great idea, but perhaps a halfway house with some asynchronous logging happening - you fire details off to a logging class, that every now and then logs the details you are after - still not 100% solid if the app crashes, but you're less likely to lose everything.

这篇关于&QUOT; HttpContext.Current.Session&QUOT; VS的Global.asax&QUOT; this.Session&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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