如何设置自定义服务器变量? [英] How does one set a custom server variable?

查看:90
本文介绍了如何设置自定义服务器变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web应用程序中,我测试一页上的状况,并希望将测试结果保留指定的持续时间(例如5分钟).我当然可以使用cookie来做到这一点,但是以防万一用户禁用了cookie,我认为自定义服务器变量可以解决这个问题.我已经听说过,但是还没有找到解决方法.

In my web application I test for a condition on one page and want to retain the results of the test for a specified duration (such as 5 minutes). I can do this with a cookie, of course, but just in case a user has disabled cookies, I'm thinking a custom server varible might do the trick. I've heard about this, but I haven't been able to find out to do it.

或者,也许会话变量可能更好?没有固有的方法可以使这些过期,对吗?这样的变量在IIS抛出之前将存活多长时间?

Alternatively, maybe a session variable might be better? There's no inherent way to expire these, right? How long would such a variable survive before IIS tosses it?

推荐答案

Asp.Net具有会话状态.每个Asp.Net页面都有一个 Session 属性.您也可以通过 HttpContext.Current.Session 访问它,尽管有细微的差别,直到被您咬住才变得无关紧要(请参阅 Session和HttpContext.Current.Session之间的区别 .

Asp.Net has session state. Each Asp.Net Page has a Session property. You can also access it via HttpContext.Current.Session, though there is a subtle difference that won't matter until it bites you (which see my answer to the question, Difference between Session and HttpContext.Current.Session.

默认会话状态配置为内存中.这意味着,如果您的网站是由F5 Networks Big IP(负载均衡器)群集提供服务的,除非负载均衡器设置为粘性"(意味着一旦分配给群集中的计算机,您就可以除非机器出现故障或以其他方式从群集中删除,否则它总是会命中该机器.

The default session state configuration is in-memory. That means that if your web site is served up from something like an F5 Networks Big IP (load balancer) cluster, unless the load balancer is set up to be 'sticky' (meaning that once assigned to a machine in the cluster, you'll always hit that machine unless it goes down or is otherwise removed from the cluster).

如果您的群集网站没有粘性会话,则当您的请求命中另一台计算机时,会话状态将消失...然后当另一个请求命中原始计算机时,该会话状态将重新出现.您可以猜测会带来多少乐趣.

If your clustered web site doesn't have sticky sessions, your session state will vanish when your request hits a different machine...and then reappear when another request hits the original machine. You can guess how much fun that can be.

您可以在web.config中启用对SQL Server会话的内置支持.以这种方式设置后,您将使用SQL Server作为会话状态的后备存储.这样可以解决上面提到的服务器场问题,但这样做的代价是每次访问会话状态值时,每次每次都命中一个SQL Server.这会大大降低访问速度,尤其是在会话大小较大的情况下.奇怪的是,每次访问会话状态设置时,执行SQL查询并在网络上推送500kb会话博客都会对性能产生负面影响.谁知道?[不要问我们怎么发现的.]

There is built-in support for SQL Server session that you can enable in your web.config. Once set up that way, you're using SQL Server as the backing store for session state. That solves the server farm issue noted above, but does so at the expense of hitting a SQL Server each and every time a session state value is accessed. That slows down access considerable, especially if the session size large. Oddly, executing a SQL query and shoving a 500kb session blog across the network every time you access a session state setting can negatively impact performance. Who knew? [Don't ask how we found that out].

还有其他解决方案: ScaleOut的产品(例如, ScaleOut SessionServer ).

There are other solutions to the problem: ScaleOut's products come to mind (e.g., ScaleOut SessionServer).

或者您可以使用某些内容滚动自己的会话状态提供程序就像AppFabric缓存一样.

Or you can roll your own session state provider using something like an AppFabric cache.

使用SQL Server会话状态的另一个功能"是,进入会话的所有内容都必须是 ISerializable .

Another "feature" of using SQL Server sessions state is that anything going into Session must be ISerializable.

这篇关于如何设置自定义服务器变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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