關於HttpHandler的相關知識總結


一、關於IHttpHandler.IsReusable

  很多人不明白,這哥們到底干嘛的,估計是微軟最初的一個想法--讓一個對象可以一直不斷地被重復使用

,但這個想法不成熟,會帶來很多隱藏問題,一個對象作為始終存在的對象,只要被污染了,它就不能正常使用了.

所以,我們會看見微軟自己也一直讓這個屬性值為false;

二、關於ashx的Handler執行方式

  擴展名為ashx的請求是通過SimpleHandlerFactory處理程序工廠完成的,當請求一個ashx擴展名的服務器上資源時,SimpleHandlerFactory將找到對應的ashx文件,通過這個文件找到對應的處理程序。最后,SimpleHandlerFactory通過反射創建一個此類型處理程序對象實例

using System;
using System.Runtime;
using System.Web.Compilation;
namespace System.Web.UI
{
    internal class SimpleHandlerFactory : IHttpHandlerFactory2, IHttpHandlerFactory
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        internal SimpleHandlerFactory()
        {
        }
        public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
        {
            return ((IHttpHandlerFactory2)this).GetHandler(context, requestType, VirtualPath.CreateNonRelative(virtualPath), path);
        }
        IHttpHandler IHttpHandlerFactory2.GetHandler(HttpContext context, string requestType, VirtualPath virtualPath, string physicalPath)
        {
            BuildResultCompiledType buildResultCompiledType = (BuildResultCompiledType)BuildManager.GetVPathBuildResult(context, virtualPath);
            Util.CheckAssignableType(typeof(IHttpHandler), buildResultCompiledType.ResultType);
            return (IHttpHandler)buildResultCompiledType.CreateInstance();
        }
        public virtual void ReleaseHandler(IHttpHandler handler)
        {
        }
    }
}

三、HttpHandlerFactory到底是何物?

  從上面的ashx可以得知,ASP.NET實際不會將指定格式的HTTP請求直接定位到具體的IHttpHandler容器之上,而定位到了其內部默認的IHttpHandlerFactory上。

IHttpHandlerFactory的作用是對IHttpHandler容器進行調度和管理。

  如果我們自定義的HttpHandler比較多,會在Web.config中注冊很多HttpHandler類,這時把這些HttpHandler通過一個HttpHandlerFactory來集中管理就顯的非常必要,

此時只需要在Web.config中注冊此HttphandlerFactory即可。

  IHttpHandlerFactory接口包含兩個方法。GetHandler返回實現IHttpHandler接口的類的實例,ReleaseHandler使工廠可以重用現有的處理程序實例

實現一個自定義的HttphandlerFactory同樣需要兩步:

  第一:定義一個實現了IHttpHandlerFactory接口的類,並實現其GetHandler方法。在GetHandler中,我們可以根據具體業務選擇不同的工廠模式實現方式,下面的代碼中包括了通過判斷后綴實現的簡單工廠模式和通過反射實現的工廠模式兩種實現方式。

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;


namespace MyHttpHandler
{
    public class MyHttpHandlerFactory:IHttpHandlerFactory
    {
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            #region 通過判斷后綴實現簡單工廠模式
            //獲取文件服務器物理路徑
            string path = context.Request.PhysicalPath;
            //獲取文件名后綴名
            string exstention = Path.GetExtension(path).ToLower();

            if (exstention == ".html")
            {
                return new HtmlHttpHandler();
            }
            else if (exstention == ".xml")
            {
                return new XMLHttpHandler();
            }
            else
            {
                return null;
            }
            #endregion

            #region 通過反射實現工廠模式
            string handlerName = url.Substring(url.LastIndexOf("/") + 1);
            string className = handlerName.Substring(0, handlerName.IndexOf("."));
            string fullClassName = "MyHttpHandler." + className;

            object h = null;

            // 采用動態反射機制創建相應的IHttpHandler實現類。
            h = Activator.CreateInstance(Type.GetType(fullClassName));

            return (IHttpHandler)h;

            #endregion

        }

        public void ReleaseHandler(IHttpHandler handler) 
        {
            
        }
    }
}
View Code

 

四、求關注、求推薦

        兄台給點鼓勵吧 O(∩_∩)O~,你的鼓勵是我繼續寫好這一系列的理由

五、下一篇

 你必須知道的ASP.NET-----IHttpAsyncHandler實質


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM