HttpContext & HttpRuntime
问题引出
HttpContext.Current.Cache .VS. HttpRuntime.Cache
- HttpRuntime.Cache:获取当前应用程序的Cache
- HttpContext.Current.Cache:为当前HTTP请求获取Cache对象,跨线程可用,在APM模式或创建了子线程的场景中不能用
HttpContext.Current.Cache 调用 HttpRuntime.Cache实现,两者指向同一 Cache对象:
- HttpRuntime.Cache相当于是缓存具体实现类,非Web应用下也可以使用
- HttpContext.Current.Cache是对缓存类的封装,局限HttpContext只能用于Web应用
在某些情况下,HttpContext还没被创建出来为null,就只能用 HttpRuntime.Cache。
- 定时器的回调。
- Cache的移除通知。
- APM模式下异步完成回调。
- 主动创建线程或者将任务交给线程池来执行
建议使用 HttpRuntime.Cache,既能减少出错,也减少一次函数调用。
eg:在获取程序\站点的根目录时尽量使用 System.Web.HttpRuntime.AppDomainAppPath。
其中,关于 APM模式 参见:https://www.cnblogs.com/mcgrady/p/5660314.html
HttpContext
限制:同一Http请求
Server.Transfer .VS. Response.Redirect
Server.Transfer 请求跳转页面,归属同一个http请求,URL无变化,所有操作均在服务器完成;Response.Redirect 则是重新发起新的页面请求,需要浏览器和服务器交互。
具体参见:HttpContext 初识;
HttpContext.Current 由其维护
System.Runtime.Remoting.Messaging.CallContext.HostContext as HttpContext
其中,CallContext.HostContext 表示:获取或设置与当前线程相关联的主机上下文 。
APM模式下的异步完成回调,访问HttpContext.Current方法:推荐第2种方法
- 在类型中添加一个字段来保存HttpContext的引用(异步开始前)
- 将HttpContext赋值给BeginXXX方法的最后一个参数(object state)
HttpRuntime
使用时建议自行封装,可参见:ASP.Net HttpRuntime.Cache 使用总结;
获取或设置与当前线程相关联的主机上下文
HttpContext & HttpRuntime的更多相关文章
- System.Web.NullPointerException
在.Net异步webApi中我们需要记录日志信息,需要获取客户端的ip地址,我们需要使用:HttpContext.Current.Request.ServerVariables["REMOT ...
- 缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cache. 我们再用. ...
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cac ...
- HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...
- [摘]HttpContext, HttpRequest, HttpResponse, HttpRuntime, HttpServerUtility
[摘]http://www.cnblogs.com/fish-li/archive/2011/08/21/2148640.html HttpRuntime HttpRuntime公开了一个静态方法 U ...
- HttpContext.Current.Cache 和 HttpRuntime.Cache
HttpRuntime.Cache:用于winfrom 和 web HttpContext.Current.Cache 用于web .NET中Cache有两种调用方式:HttpContext.Curr ...
- Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/avon520/archive/2009/11/25/4872704.aspx .NET中Cache有两种调用方式:Ht ...
- HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cac ...
- Asp.Net framework 类库 自带的缓存 HttpRuntime.Cache HttpContext.Cache
两个Cache 在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序 ...
随机推荐
- k8s-基础环境配置(六)
hostname配置1)修改主机名hostnamectl set-hostname xxx2)加入主机映射vim /etc/hosts……关闭selinuxsed -i '/^SELINUX/s/=. ...
- 基于ADO的远程Oracle连接
最近在一个通过MFC做一个界面,通过这个界面可以对布置在另一台服务器上的数据库MySQL.SQl Server.Oracle进行增删创建表的操作.其中我通过ADO很快就完成了对MySQL和SQL Se ...
- 学习activiti的时候使用truncate命令清空表
参考文档: https://blog.csdn.net/iw1210/article/details/79586033 https://www.cnblogs.com/hougang/p/mysql_ ...
- angular2 ng-if
ng-if <td ><div class="td-li" > <a (click)="open(i)" class=" ...
- Python3 IO编程之StringIO和BytesIO
StringIO 很多时候,数据读写不一定是文件,也可以在内存中读写. 要把str写入StringIO,我们需要先创建一个StringIO,然后像文件一样写入即可 >>> from ...
- C# sqlsugar依赖引用报错的问题解决
English Message : You need to refer to MySql.Data.dll↵Chinese Message : 需要引用MySql.Data.dll,请在Nuget安装 ...
- Eureka&Zookeeper&Consul 原理与对比
CAP 定理CAP定理:CAP定理又称CAP原则,指的是在一个分布式系统中,一致性(Consistency).可用性(Availability).分区容错性(Partition tolerance). ...
- (简单实用)Android支付宝商家收款语音播报
支付宝商家收款时,语音提示:支付宝收款xxx元,当时觉得这东西还挺有趣的,第一时间通知给商家,减少不必要的纠纷,节约时间成本,对商家对用户都挺好的. 在商家版有这样收款播报的功能,我觉得挺好的. 对列 ...
- Appium移动自动化测试-----(九) appium API 之应用操作
1.安装应用 方法: installApp() 安装应用到设备中去.需要apk包的路径. driver.installApp("path/to/my.apk"); driver.i ...
- 构造几个死锁的例子(Java )
目录 构造几个死锁的例子(Java ) 1 2 3 构造几个死锁的例子(Java ) 1 public class Deadlock { public static void main(String[ ...