【原创】cs+html+js+css模式(三):RemoteCallHandler详解

1.         回顾

上一篇文章,将webconfig中的设置介绍了下,为我们在获取数据处理的时候,打好了扎实的基础,现在我们我们来详细讲讲“RemoteCallHandler”这个类。

2.         介绍

RemoteCallHandler类,是我们在webconfig中定义了,当Post访问。Xyz后缀的文件时,网站程序自己调用的,前面也说过,此类继承了两个类一个是IHttpHandler,一个是IRequiresSessionState;

IRequiresSessionState类:

指定目标 HTTP 处理程序需要对会话状态值具有读写访问权。这是一个标记接口,没有任何方法。在自定义 HTTP 处理程序中实现 IRequiresSessionState 接口,以确定处理程序是否需要对会话状态值具有读写访问权。

IHttpHandler类:

       定义 ASP.NET 为使用自定义 HTTP 处理程序同步处理 HTTP Web 请求而实现的协定。可以用任何符合公共语言规范 (CLS) 的语言编写自定义 HTTP 处理程序来处理特定的、预定义类型的 HTTP 请求。响应这些特定请求的是在 HttpHandler 类中定义的可执行代码,而不是常规的 ASP 或 ASP.NET网页。HTTP 处理程序向您提供一种方法,使您可以与 IIS Web 服务器的低级别的请求和响应服务交互,同时提供极其类似于 ISAPI 扩展但编程模型较为简单的功能。如果您的处理程序将访问会话状态值,它必须实现 IRequiresSessionState 接口(不包含任何方法的标记接口)。

从MSDN介绍可得到,我们在自定义IHttpHandler的时候必须要继承这两个类,为什么要使用此类,因为我们要获取网站请求的数据,然后根据捕获访问的数据,对数据进行相关的调用与设置。

3.         RemoteCallHandler类详细

1.      using System;

2.      using System.Reflection;

3.      using System.Web;

4.      using System.Web.SessionState;

5.       

6.      namespace Better.Web

7.      {

8.      //此处继承IhttpHandler与IRequiresSessionState

9.      public class RemoteCallHandler : IHttpHandler, IRequiresSessionState

10.   {

11.    

12.   #region IHttpHandler 成员

13.   //实现IhttpHandler里面的IsResuable属性

14.   public bool IsReusable { get { return false; } }

15.    

16.   //实现IHttpHandler中的ProcessRequest方法

17.   public void ProcessRequest(HttpContext context)

18.   {

19.   //获取当前请求对象

20.   HttpRequest request = context.Request;

21.   //获取当前发送对象

22.   HttpResponse response = context.Response;

23.   //获取请求的头

24.   string invokeType = request.Headers["RemoteInvoke"];

25.   //如果请求的头位空,直接抛出异常

26.   if (string.IsNullOrEmpty(invokeType)) { throw new MissingInvokeTypeException(); }

27.   //判断当前的指定头的值是否是正确的

28.   if (invokeType.Equals("MethodInvoke"))

29.   {

30.        try

31.        {

32.            //如果请求符合,运行的方法,打印在页面上

33.            response.Write(MethodInvoke(request));

34.        }

35.        catch (Exception ex)

36.        {

37.             /*当报错时,处理的信息,将信息构造成json格式*/

38.             string message = (ex.InnerException == null ? ex.Message : ex.InnerException.Message);

39.             response.StatusCode = 500;

40.             response.Write("{");

41.             response.Write(string.Format("\"code\":{0},\"message\":\"{1}\"", 500, message));

42.            response.Write("}");

43.         }

44.         finally

45.        {

46.              try

47.             {

48.                   response.End();

49.             }

50.             catch { }

51.         }

52.   }

53.   }

54.   #endregion

55.   //真实的处理函数

56.   private string MethodInvoke(HttpRequest request)

57.   {

58.   /*判断请求是否为空*/

59.   if (request == null) { throw new ArgumentNullException("request"); }

60.   /*获取反射对象,判断request.Headers["Assembly"]是否为null 如果为null直接返回空字符串*/

61.   string assembly = request.Headers["Assembly"] ?? "";

62.   /*获取操作类型*/

63.   string typeName = request.Headers["TargetType"];

64.   /*获取要调用的后台函数名*/

65.   string methodName = request.Headers["CallingMethod"];

66.   /*判断操作类型名是否为空*/

67.   if (string.IsNullOrEmpty(typeName)) { throw new MissingCallingTypeException(); }

68.   /*判断调用的后台函数名是否为空*/

69.   if (string.IsNullOrEmpty(methodName)) { throw new MissingCallingMethodException(); }

 

70.   /* 获取远程调用的目标类型。*/

71.   Type type = Type.GetType(typeName + (string.IsNullOrEmpty(assembly) ? "" : "," + assembly));

72.   if (type != null)

73.   {

74.   /* 获取远程调用的目标方法。*/

75.   MethodInfo mi = type.GetMethod(methodName);

76.   if (mi != null)

77.   {

78.   /* 获取调用参数。*/

79.   object[] arguments = null;

80.   ParameterInfo[] pis = mi.GetParameters();

81.   if (pis.Length > 0)

82.   {

83.           arguments = new object[pis.Length];

84.           for (int i = 0; i < pis.Length; i++)

85.          {

86.             arguments[i] = Convert.ChangeType(request.Form[i], pis[i].ParameterType);

87.          }

88.   }

89.   /* 判断调用的方法是否静态方法。

l        如果是静态方法,则方法调用不需创建类实例。*/

90.   object inst = mi.IsStatic ? null : Activator.CreateInstance(type, true);

91.   if (mi.ReturnType.Name == "Void")

92.   {

93.           mi.Invoke(null, arguments);

94.           return "";

95.   }

96.   else

97.   {

98.           return mi.Invoke(null, arguments).ToString();

99.   }

100.}

101.throw new GetMethodFailedException(typeName, methodName);

102.}

103.throw new GetTypeFailedException(typeName);

104.}

105.}

 

106./// <summary>

107./// 远程调用缺少调用类型时引发的异常。

108./// </summary>

109.[Serializable]

110.public class MissingInvokeTypeException : Exception

111.{

112.       public MissingInvokeTypeException() : base("缺少调用类型。") { }

113.}

 

114./// <summary>

115./// 远程调用缺少目标类型时引发的异常

116./// </summary>

117.[Serializable]

118.public class MissingCallingTypeException : Exception

119.{

120.       public MissingCallingTypeException() : base("远程调用缺少目标类型。") { }

121.}

 

122./// <summary>

123./// 远程调用缺少目标方法时引发的异常

124./// </summary>

125.[Serializable]

126.public class MissingCallingMethodException : Exception

127.{

128.        public MissingCallingMethodException() : base("远程调用缺少目标方法。") { }

129.}

 

130./// <summary>

131./// 远程调用加载类型失败时引发的异常。

132./// </summary>

133.[Serializable]

134.public class GetTypeFailedException : Exception

135.{

136.       public GetTypeFailedException() : base("加载类型失败。") { }

137.       public GetTypeFailedException(string typeName) : base(string.Format("加载类型 \"{0}\" 失败。", typeName)) { }

138.}

 

139./// <summary>

140./// 远程调用获取目标方法失败时引发的异常。

141./// </summary>

142.[Serializable]

143.public class GetMethodFailedException : Exception

144.{

145.           public GetMethodFailedException() : base("获取方法失败。") { }

146.           public GetMethodFailedException(string methodName) : base(string.Format("获取 \"{0}\" 方法失败。", methodName)) { }

147.           public GetMethodFailedException(string typeName, string methodName) : base(string.Format("获取类型 \"{0}\" 的 \"{1}\" 方法失败。", typeName, methodName)) { }

148.}

 

149./// <summary>

150./// 远程调用失败时引发的异常。

151./// </summary>

152.[Serializable]

153.public class RemoteInvokeFailedException : Exception

154.{

155.     public RemoteInvokeFailedException() : base("远程调用失败。") { }

156.     public RemoteInvokeFailedException(string typeName, string methodName) : base(string.Format("远程调用方法 \"{0}.{1}\" 失败。", typeName, methodName)) { }

157.}

158.}

posted @ 2010-01-20 14:57  NoRoadZH  阅读(367)  评论(0编辑  收藏  举报