ASP.NET HttpApplication HttpModule + Page Life Cycle Events Sequence Test


/*
http://msdn.microsoft.com/zh-cn/library/bb470252
http://msdn.microsoft.com/zh-cn/library/ms178472
*/
namespace Microshaoft
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Threading;
    using System.Reflection;
    public class TestHttpModule : IHttpModule
    {
        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此模块,
        /// 并向 IIS 注册此模块,然后才能使用。有关详细信息,
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule Members
        public void Dispose()
        {
            //此处放置清除代码。
        }
        //private HttpContext _httpContext;
        //private HttpRequest _httpRequest;
        //private HttpResponse _httpResponse;
        public void Init(HttpApplication httpApplication)
        {
            // 下面是如何处理 LogRequest 事件并为其
            // 提供自定义日志记录实现的示例
            var httpContext = httpApplication.Context;
            //return;
            var eventsInfosNamesBlackList = new List<string>()
                                                        {
                                                            "MapRequestHandler"
                                                            , "LogRequest"
                                                        };
            var eventsInfos = typeof(HttpApplication)
                                        .GetEvents()
                                        .ToList()
                                        .Where
                                            (
                                                (x) =>
                                                {
                                                    return
                                                        !eventsInfosNamesBlackList.Any
                                                                                    (
                                                                                        (xx) =>
                                                                                        {
                                                                                            return x.Name.ToLower().Contains(xx.ToLower());
                                                                                        }
                                                                                    );
                                                        ;
                                                }
                                            ).ToArray();
            //Exception Details: System.PlatformNotSupportedException: This operation requires IIS integrated pipeline mode.
            ///            httpApplication.MapRequestHandler += new EventHandler(HttpApplication_EventHandlerProcess);
            ///            httpApplication.PostMapRequestHandler += new EventHandler(HttpApplication_EventHandlerProcess);
            ///            httpApplication.LogRequest += new EventHandler(HttpApplication_EventHandlerProcess);
            ///            httpApplication.PostLogRequest += new EventHandler(HttpApplication_EventHandlerProcess);
            long i = 1;
            Array.ForEach
                    (
                        eventsInfos
                        ,
                        (x) =>
                        {
                            Console.WriteLine(x.Name);
                            x.AddEventHandler
                            (
                                httpApplication
                                , new EventHandler
                                        (
                                            (xx, y) =>
                                            {
                                                Console.WriteLine(x.Name);
                                                var application = xx as HttpApplication;
                                                //var application = sender as HttpApplication;
                                                var context = application.Context;
                                                var request = context.Request;
                                                var response = context.Response;
                                                //Exception Details: System.PlatformNotSupportedException: This operation requires IIS integrated pipeline mode.
                                                //response.Write(context.CurrentNotification + "<br>");
                                                var s = string.Format
                                                                (
                                                                    "{1}{0}{2}{0}{3}"
                                                                    , " : "
                                                                    , "HttpModule"
                                                                    , i++
                                                                    , x.Name
                                                                );
                                                response.Write(s + "<br>");
                                                HttpApplication_EventHandlerProcess
                                                                        (
                                                                            xx
                                                                            , y
                                                                            , x.Name
                                                                        );
                                                try
                                                {
                                                    //application.Response.End();
                                                }
                                                catch (ThreadAbortException tae)
                                                {
                                                }
                                            }
                                        )
                            );
                        }
                    );
        }
        void HttpApplication_EventHandlerProcess
                                        (
                                            object sender
                                            , EventArgs e
                                            , string remark = null        //string.Empty
                                        )
        {
            var application = sender as HttpApplication;
            //var application = sender as HttpApplication;
            var context = application.Context;
            var request = context.Request;
            var response = context.Response;
            //response.Write("Remark: " + remark + "<br>");
            //response.Write(context.CurrentNotification + "<br>");
        }
        #endregion
    }
}
namespace Microshaoft
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Web;
    using System.Web.UI;
    public partial class PageLifeCycleTest : Page
    {
        static long _f1;
        long _f2;
        override protected void OnInit(EventArgs e)
        {
            _f1++;
            _f2++;
            var eventsInfosNamesBlackList = new List<string>()
                                                        {
                                                            "MapRequestHandler"
                                                            , "LogRequest"
                                                            , "Unload"
                                                        };
            var eventsInfos = typeof(Page)
                                        .GetEvents()
                                        .ToList()
                                        .Where
                                            (
                                                (x) =>
                                                {
                                                    return
                                                        !eventsInfosNamesBlackList.Any
                                                                                    (
                                                                                        (xx) =>
                                                                                        {
                                                                                            return x.Name.ToLower().Contains(xx.ToLower());
                                                                                        }
                                                                                    );
                                                }
                                            ).ToArray();
            short i = 1;
            Array.ForEach
                    (
                        eventsInfos
                        ,
                        (x) =>
                        {
                            Console.WriteLine(x.Name);
                            x.AddEventHandler
                            (
                                this
                                , new EventHandler
                                        (
                                            (xx, y) =>
                                            {
                                                Console.WriteLine(x.Name);
                                                var page = xx as Page;
                                                //Exception Details: System.PlatformNotSupportedException: This operation requires IIS integrated pipeline mode.
                                                //response.Write(context.CurrentNotification + "<br>");
                                                var s = string.Format
                                                                (
                                                                    "{1}{0}{2}{0}{3}"
                                                                    , " : "
                                                                    , "Page"
                                                                    , i++
                                                                    , x.Name
                                                                );
                                                //if (Response != null)
                                                {
                                                    Response.Write(s + "<br>");
                                                    //Response.Write("static: " + _f1 + "<br>");
                                                }
                                                s = string.Format
                                                                (
                                                                    "{1}{0}{2}{0}{3}{0}{4}"
                                                                    , " : "
                                                                    , "Page Static Member"
                                                                    , _f1
                                                                    , "Page Instance Member"
                                                                    , _f2
                                                                );
                                                //if (Response != null)
                                                {
                                                    Response.Write(s + "<br>");
                                                    //Response.Write("static: " + _f1 + "<br>");
                                                }
                                                try
                                                {
                                                    //application.Response.End();
                                                }
                                                catch (ThreadAbortException tae)
                                                {
                                                }
                                            }
                                        )
                            );
                        }
                    );
            base.OnInit(e);
        }
    }
}
/* http://local.asp.net/pagelifecycletest.aspx
<%@ Page
    ValidateRequest="false"
    Debug="true"
    ContentType="text/html;charset=UTF-8"
%>
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Linq" %>
    <%@ Import Namespace="System.Threading" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Web.UI" %>
    <script language="C#" runat="server">
        static long _f1;
        long _f2;
        override protected void OnInit(EventArgs e)
        {
            _f1 ++;
            _f2 ++;
            var eventsInfosNamesBlackList = new List<string>()
                                                        {
                                                            "MapRequestHandler"
                                                            , "LogRequest"
                                                            , "Unload"
                                                        };
            var eventsInfos = typeof(Page)
                                        .GetEvents()
                                        .ToList()
                                        .Where
                                            (
                                                (x) =>
                                                {
                                                    return
                                                        !eventsInfosNamesBlackList.Any
                                                                                    (
                                                                                        (xx) =>
                                                                                        {
                                                                                            return x.Name.ToLower().Contains(xx.ToLower());
                                                                                        }
                                                                                    );
                                                }
                                            ).ToArray();
            short i = 1;
            Array.ForEach
                    (
                        eventsInfos
                        ,
                        (x) =>
                        {
                            Console.WriteLine(x.Name);
                            x.AddEventHandler
                            (
                                this
                                , new EventHandler
                                        (
                                            (xx, y) =>
                                            {
                                                Console.WriteLine(x.Name);
                                                var page = xx as Page;
                                                //Exception Details: System.PlatformNotSupportedException: This operation requires IIS integrated pipeline mode.
                                                //response.Write(context.CurrentNotification + "<br>");
                                                var s = string.Format
                                                                (
                                                                    "{1}{0}{2}{0}{3}"
                                                                    , " : "
                                                                    , "Page"
                                                                    , i++
                                                                    , x.Name
                                                                );
                                                //if (Response != null)
                                                {
                                                    Response.Write(s + "<br>");
                                                    //Response.Write("static: " + _f1 + "<br>");
                                                }
                                                s = string.Format
                                                                (
                                                                    "{1}{0}{2}{0}{3}{0}{4}"
                                                                    , " : "
                                                                    , "Page Static Member"
                                                                    , _f1
                                                                    , "Page Instance Member"
                                                                    , _f2
                                                                );
                                                //if (Response != null)
                                                {
                                                    Response.Write(s + "<br>");
                                                    //Response.Write("static: " + _f1 + "<br>");
                                                }
                                                try
                                                {
                                                    //application.Response.End();
                                                }
                                                catch (ThreadAbortException tae)
                                                {
                                                }
                                            }
                                        )
                            );
                        }
                    );
            base.OnInit(e);
        }
    </script>
*/
/* http://local.asp.net/pagelifecycletest1.aspx
<%@ Page
    ValidateRequest="false"
    Debug="true"
    Inherits="Microshaoft.PageLifeCycleTest"
    ContentType="text/html;charset=UTF-8"
%>
*/
/* http://local.asp.net/pagelifecycletest1.aspx
<%@ Page
    ValidateRequest="false"
    Debug="true"
    Inherits="Microshaoft.PageLifeCycleTest"
    ContentType="text/html;charset=UTF-8"
%>
*/
/* Web.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <modules>
            <add name="TestHttpModule" type="Microshaoft.TestHttpModule, TestHttpModule" />
        </modules>
    </system.webServer>
    <system.web>
        <httpModules>
            <add name="TestHttpModule" type="Microshaoft.TestHttpModule, TestHttpModule" />
        </httpModules>
    </system.web>
</configuration>
*/

posted @ 2012-08-15 00:54  于斯人也  阅读(826)  评论(0编辑  收藏  举报