ASP.Net一般处理程序Session用法

1.在aspx和aspx.cs中,都是以 Session["type"]="aaa" 和 string aaa=Session["type"].ToString() 或使用

HttpContext.Current.Session[strSessionName] = strValue;进行读写。

而在一般处理程序ashx中,Session都要使用context.Session,读写方法不变。

2.在ashx文件中,若要对Session进行成功的读写,要添加命名空间和接口,否则context.Session["type"]读出的总是null。

命名空间:using System.Web.SessionState

增加接口:IRequiresSessionState

代码如下:

public class pagingQuery : IHttpHandler, IRequiresSessionState

string type =context.Session["type"].ToString();

刚刚接触.net web端的朋友都会被Session坑过,莫名其妙的不能读取Session数据,后来知道原来有IRequiresSessionState这个接口,不继承的就不能读取Session里面的数据,知道这个以后呢,也不清楚里面具体是如何实现的。对此一直不甘心,于是查了各方面的资料终于模拟出来了。

  在一般处理程序(ashx文件)里面有个一个(HttpContext Context),F12进入HttpContext 类你面你会发现它应该是用了单例的模式,里面有个 public static HttpContext Current { get; set; },应该是确定程序只有一个上下文。接下来可以找到public HttpSessionState Session { get; },这就是我们需要读取Session。
废话少说,首先说明用到了反射。我们来介绍下Type 类中的Type IsAssignableFrom(Type c);方法。假设A类继承了B接口,  Type a = typeof(A);  Type b = typeof(B); 那么 a. IsAssignableFrom(b)的值为ture;这个可以判断类是否继承了IRequiresSessionState。这是第一步。
  第二步就是找到当前访问Session的类。这个就要用到StackTrace类,从名字上来看这个类是用来跟踪代码的。这里面要用到StackTrace 的GetFrame(index)方法和GetMethod(); 。GetFrame(index)这个是从调用的最里层往外层遍历,它的返回值也是StackTrace 。是GetMethod() 返回值是MethodBase,而MethodBase的ReflectedType属性可以得到当前类的Type。
  原理都在上面的,下面的代码是模拟过程。
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Diagnostics;
using System.Reflection;
using System.Web.SessionState;
 
namespace Ztest
{
    public class Program: IRequiresSessionState
    {
        public static void Main(string[] args)
        {
            try
            {
                if (Test.Current.session == null)
                {
                    Console.WriteLine("没有继承IRequiresSessionState");
                }
                else
                {
                    Console.WriteLine(Test.Current.session);
                }
            }
            catch (Exception ex)
            {
            }
            Console.ReadLine();
        }
    }
    public class Test
    {
        private  Test()
        {
            Type basetype = typeof(IRequiresSessionState);
            StackTrace trace = new StackTrace();
            int i = 0;
            Type type;
            while (true)
            {
                ///找到外层第一个调用类
                MethodBase methodName = trace.GetFrame(i).GetMethod();
                type = methodName.ReflectedType;
                if (type != typeof(Test))
                {
                    break;
                }
                i++;
            }
           
            Boolean key = basetype.IsAssignableFrom(type);
            if (key)
            {
                session = _m;
            }
            else
            {
                session = null;
            }
        }
        private static Test _Current;
        private string _m = "当前类实现了IRequiresSessionState";
        /// <summary>
        /// 模拟session
        /// </summary>
        public Object session { get; set; }
        public static Test Current
        {
            get
            {
                return get();
            }
            set
            {
                Current = value;
            }
        }
        private static Test get()
        {
            if (_Current == null)
            {
                _Current = new Test();
              
            }
            return _Current;
        }
    }
    
}

  

原文地址:https://www.cnblogs.com/qqhfeng/p/11260159.html