博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线程编程中用到HttpContext.Current的方法封装
阅读量:6900 次
发布时间:2019-06-27

本文共 2021 字,大约阅读时间需要 6 分钟。

  
在线程编程时能够为我们的性能提高不少,但是线程不是请求所以请求上下文我们就不能够用到!我在编程时遇到的几个地方留下个映像,同时也希望给不知道的同志们留个纪念!!!
    1.缓存(Cache):我们通常会从System.Web.HttpContext.Current.Cache获取,但是在线程中我们所得到的HttpContext.Current为null,所以我们得通过System.Web.HttpRuntime.Cache获得缓存实例,我们可以封装一个方法,这样我们就可以不用管他是在哪里都可以调用当前缓存了。代码如下:
 1 
public
 
class
 Cacher
 2 
{
 3 
    
private
 Cacher() { }
 4 
 5 
    
private
 
static
 
readonly
 Cache cache;
 6 
 7 
    
static
 Cacher() {
 8 
        HttpContext context 
=
 HttpContext.Current;
 9 
        
if
 (context 
==
 
null
)
10 
            cache 
=
 context.Cache;
11 
        
else
12 
            cache 
=
 HttpRuntime.Cache;
13 
    }
14 
}
    2.获取文件的物理路径:通常我们会用System.Web.HttpContext.Current.Request来获取当前的物理等有关路径或URL,通过System.Web.HttpContext.Current.Server.MapPath方法来获取当前文件或目录的物理路径。和上面一样在线程中这是解决不了问题的,我们可以通过应用程序域(System.AppDomain.CurrentDomain.BaseDirectory)来获得根目录。
 1 
        
public
 
static
 
string
 RootPath() {
 2 
            
return
 RootPath(
"
/
"
);
 3 
        }
 4 
 5 
        
public
 
static
 
string
 RootPath(
string
 filePath)
 6 
        {
 7 
            
string
 rootPath 
=
 AppDomain.CurrentDomain.BaseDirectory;
 8 
            
string
 separator 
=
 Path.DirectorySeparatorChar.ToString();
 9 
            rootPath 
=
 rootPath.Replace(
"
/
"
, separator);
10 
            
if
 (filePath 
!=
 
null
)
11 
            {
12 
                filePath 
=
 filePath.Replace(
"
/
"
, separator);
13 
                
if
 (((filePath.Length 
>
 
0
&&
 filePath.StartsWith(separator)) 
&&
 rootPath.EndsWith(separator))
14 
                {
15 
                    rootPath 
=
 rootPath 
+
 filePath.Substring(
1
);
16 
                }
17 
                
else
18 
                {
19 
                    rootPath 
=
 rootPath 
+
 filePath;
20 
                }
21 
            }
22 
            
return
 rootPath;
23 
        }
24 
25 
        
public
 
string
 PhysicalPath(
string
 path)
26 
        {
27 
            
return
 (RootPath().TrimEnd(
new
 
char
[] { Path.DirectorySeparatorChar }) 
                +
 Path.DirectorySeparatorChar.ToString() 
+
 path.TrimStart(
new
 
char
[] { Path.DirectorySeparatorChar }));
28 
        }
29 
30 
        
public
 
string
 MapPath(
string
 path)
31 
        {
32 
            HttpContext context 
=
 HttpContext.Current;
33 
            
if
 (context 
!=
 
null
)
34 
            {
35 
                
return
 context.Server.MapPath(path);
36 
            }
37 
            
return
 PhysicalPath(path.Replace(
"
/
"
, Path.DirectorySeparatorChar.ToString()).Replace(
"
~
"
""
));
38 
        }

    OK,暂时先逮住这两个家伙,以后发现了再补上!!!

本文转自网魂小兵博客园博客,原文链接:http://www.cnblogs.com/xdotnet/archive/2007/06/25/aspnet_threading_httpcontext.html,如需转载请自行联系原作者

你可能感兴趣的文章
mysql qps 计算_请问MYSQL数据库QPS,TPS采用哪种计算方式?
查看>>
python to_excel参数解释_pandas中read_excel() 和 to_excel()各参数详解
查看>>
python列表输出奇数_Python程序在列表中打印奇数
查看>>
mysql下存在的风险_MySQL部分表复制配置下存在的运维风险、原因及一种方案
查看>>
在python中的占位符中、请你选出不属于占位符的选项_在信息组织和存储中,最基本的单位是( )。...
查看>>
java date 转换sql date_如何将java.util.Date转换为java.sql.Date?
查看>>
java 编写cgi_编写CGI小结(Java)
查看>>
java内存泄露 垃圾回收_Java中内存泄露及垃圾回收机制
查看>>
rife java_Java世界的ruby on rails — rife (转)
查看>>
java yang模型_java 深入理解jvm内存模型 jvm学习笔记
查看>>
java 任务栏_java 如何将当前程序隐藏到任务栏(类似windows上的其他程序)
查看>>
java编写一个小学辅助软件_高分求用Java编写的一个小软件
查看>>
java 现场_面试遇到现场(我哥是李刚 java模型现场)
查看>>
java asm 书_使用ASM来书写Java代码-2(zt)续2
查看>>
java 堆栈的声明_Java 堆栈
查看>>
linux 内存管理 代码,《LINUX3.0内核源代码分析》第四章:内存管理(3)
查看>>
linux 一句话运行多,Linux运维工程师常用一句话脚本,你会几句?
查看>>
linux锁住终端会话命令,Linux终端会话实时共享(kibitz)
查看>>
夏普linux电视软件,夏普电视第三方软件安装方法,使用教程和攻略
查看>>
华为手机linux终端,华为云IoT如何让“哑”终端进化为智能终端?看完这场直播你就明白了...
查看>>