服务器端等效的HttpContext的? [英] Server-side equivalent of HttpContext?

查看:133
本文介绍了服务器端等效的HttpContext的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用当前的HttpContext存储LINQ数据上下文的web应用程序。上下文是坚持当前请求,对每个用户的基础上,每里克施特拉尔的博客

I have a web app that currently uses the current HttpContext to store a LINQ Data Context. The context is persisted for the current request, on a per user basis, per Rick Strahl's blog:

string ocKey = "ocm_" + HttpContext.Current.GetHashCode().ToString("x")  
Thread.CurrentContext.ContextID.ToString();

if (!HttpContext.Current.Items.Contains(ocKey))
{
    // Get new Data Context and store it in the HTTP Context
}

不过,我有一些脚本,从Global.asax文件执行,即没有的HttpContext的。 的HttpContext.Current为NULL ,因为服务器是一个使得请求。

However, I have some scripts that execute from the global.asax file, that don't have an HttpContext. The HttpContext.Current is NULL, because the server is the one making the "request".

有没有我可以用它来存储数据上下文等的目标?所以我不担心重新创建它,并安装/拆卸对象?我只想坚持我的流程的生命周期的背景下。

Is there an equivalent object that I can use to store the Data Context? So I don't have to worry about re-creating it, and attaching/detaching objects? I only want to persist the context for the lifetime of my processes.

更新:

我目前想在我DAL辅助类使用静态变量。在第一呼叫的在类DataContext的被实例化,并存储在静态变量的方法之一。在我的过程结束时,我叫上在DataContext调用Dispose,并设置静态变量设置为NULL的另一种方法。

I am currently trying to use a static variable in my DAL helper class. on the first call to one of the methods in the class the DataContext is instantiated, and stored in the static variable. At the end of my process, I call another method that calls Dispose on the DataContext, and sets the static variable to NULL.

推荐答案

你能不能只用一个静态变量,专门为这些脚本?这将具有相同的生命周期为的AppDomain 。你也许应该仔细考虑任何并发的担忧,但它听起来像是要保持周围值的最简单方法。

Can you not just use a static variable specifically for those scripts? That will have the same life-time as the AppDomain. You should probably think carefully about any concurrency concerns, but it sounds like the simplest way to keep a value around.

(我刚刚检查,虽然的HttpApplication 的一个实例可用于服务多个请求,每个仅提供一次一个请求 - 这表明并发请求处理中创建多个实例。我还没有证实这一点,但它听起来像它不会是安全地保存在一个实例变量。)

(I've just checked, and although one instance of HttpApplication can be used to service multiple requests, each one only serves one request at a time - which suggests that multiple instances are created for concurrent request processing. I haven't validated this, but it does sound like it wouldn't be safe to keep it in an instance variable.)

编辑:乔什的回答表明,你想这是每个线程。这听起来有点奇怪给我,除非你有一个的很多的发生的这些事件,你很可能永远只能看他们执行在不同的线程,使得整个共享业务毫无意义的。如果你真的想那种事情,我建议只使用了的HttpApplication 派生类的一个实例变量 - 整整以上:)一段所述的原因

Josh's answer suggests that you want this to be per-thread. That sounds slightly odd to me, as unless you've got a lot of these events occurring, you're quite likely to only ever see them execute on different threads, making the whole sharing business pointless. If you really do want that sort of thing, I'd suggest just using an instance variable in the HttpApplication-derived class - for exactly the reason described in the paragraph above :)

这篇关于服务器端等效的HttpContext的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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