首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

MVC 系列(7):WebActivator 實作原理深度解析

     編輯:關於ASP.NET

上一章分析動態註冊 HttpModule 的實作,本章探討基於該原理的 WebActivator 程式庫。WebActivator 提供三種組件層級屬性,允許在 HttpApplication 初始化前、後及關閉時執行指定靜態方法:

[assembly: WebActivator.PreApplicationStartMethod(typeof(A.InitClass1), "PreStart")]
[assembly: WebActivator.PostApplicationStartMethod(typeof(A.InitClass1), "PostStart")]
[assembly: WebActivator.ApplicationShutdownMethod(typeof(A.InitClass1), "ShutDown")]

且每種屬性可重複使用(不同於系統內建 PreApplicationStartMethodAttribute 僅能用一次)。

原始碼結構

下載 WebActivator 1.5 原始碼(Bitbucket),專案包含 6 個核心 .cs 檔案與 packages.config(參考 Microsoft.Web.Infrastructure.dll)。

三大屬性類別

PreApplicationStartMethodAttribute、PostApplicationStartMethodAttribute、ApplicationShutdownMethodAttribute 皆繼承 BaseActivationMethodAttribute,標記 [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)],建構子接收型別與方法名稱。

基底類別 BaseActivationMethodAttribute

public abstract class BaseActivationMethodAttribute : Attribute {
    private Type _type; private string _methodName;
    public BaseActivationMethodAttribute(Type type, string methodName) { ... }
    public Type Type { get { return _type; } }
    public string MethodName { get { return _methodName; } }
    public int Order { get; set; }
    public void InvokeMethod() {
        MethodInfo method = Type.GetMethod(MethodName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
        if (method == null) throw new ArgumentException(...);
        method.Invoke(null, null);
    }
}

提供 Order 控制同屬性多次使用時的執行順序,InvokeMethod 透過反射呼叫靜態方法。

組件擴充方法 AssemblyExtensions

static class AssemblyExtensions {
    public static IEnumerable<T> GetActivationAttributes<T>(this Assembly assembly) where T : BaseActivationMethodAttribute {
        return assembly.GetCustomAttributes(typeof(T), inherit: false).OfType<T>();
    }
}

用於從組件取得指定啟動屬性集合。WebActivator 透過這些簡潔的類別,實現了靈活的應用程式啟動/關閉鉤子機制,廣泛用於 MVC、Web API 等框架的套件初始化。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved