HttpHandler和ashx要实现IRequiresSessionState接口才能访问Session信息(转载)

通常我们经常,通过session判定用户是否登录。还有一些临时的、重要的数据也尝尝存放在Session中。

在页面我们很容易的得到Session的值,但在类中就会遇到一些问题。也知道通过下面的方法得到。

System.Web.HttpContext.Current.Session["userinfo"];

 

但是今天此种方法也失灵了。在做一个小应用时,需要实现IHttpHandler,同时也需要用到用户的标识。但是在这个类中怎么也不能找到Session的值,曝出

System.Web.HttpContext.Current.Session为null

 

为什么得到的Session会是空呢?想了好久也没想通。找了好久,才找到了高人的指点,问题得到了解决。

 

解决方法:

在实现IHttpHandler的同时,也要实现IRequiresSessionState接口,其命名空间为:System.Web.SessionState。

public class WatermarkHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    //WatermarkHandler 中的代码
}

 

继续追踪:

  为什么要实现这个接口呢?这个接口是做什么用的呢?继续追踪,MSDN给了最终解释。

  IRequiresSessionState

  指定目标 HTTP 处理程序需要对会话状态值具有读写访问权。这是一个标记接口,没有任何方法。

  作用:

  在自定义 HTTP 处理程序中实现 IRequiresSessionState 接口,以确定处理程序是否需要对会话状态值具有读写访问权

    

所以记得哦,如果在自定义HTTP处理程序中,要访问Session,记得一定要实现这个接口哦。

IRequiresSessionState接口控制

刚刚接触.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 { getset; }
        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/linxingxunyan/p/5782172.html

 刚刚接触.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 { getset; }
        public static Test Current
        {
            get
            {
                return get();
            }
            set
            {
                Current = value;
            }
        }
        private static Test get()
        {
            if (_Current == null)
            {
                _Current = new Test();
              
            }
            return _Current;
        }
    }
    
}

  

posted @ 2018-02-03 19:01  核电站  阅读(371)  评论(0编辑  收藏  举报