簡體   English   中英

ASP.Net 的生命周期事件在哪里聲明? (不是事件處理程序)

[英]Where are the life cycle events of ASP.Net declared? (NOT the event handlers)

我有一個 ASP.Net MVC 應用程序。 里面有大名鼎鼎的Global.asax(我沒碰):

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

現在,人們說這是應用程序啟動時觸發的事件。 很抱歉,但我在這里看到的不是事件 - 這是一種方法。 我相信某處有一個事件正在被觸發,並且指向這個方法(所以這個方法是一個事件處理程序。我錯了嗎?)

我的 Q 是事件本身在哪里? 我在所有解決方案中尋找它並沒有找到它。

在獲得堆棧跟蹤導致對 Application_Start 的調用之后,我發現它就像

at WebApplication1.MvcApplication.Application_Start() in C:\Users\Elite\source\repos\WebApplication2\WebApplication1\Global.asax.cs:line 16
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at System.Web.HttpApplication.InvokeMethodWithAssert(MethodInfo method, Int32 paramCount, Object eventSource, EventArgs eventArgs)
   at System.Web.HttpApplication.ProcessSpecialRequest(HttpContext context, MethodInfo method, Int32 paramCount, Object eventSource, EventArgs eventArgs, HttpSessionState session)
   at System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app)
   at System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers)
   at System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context)
   at System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context)
   at System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)

這個鏈接包含整個事情的源代碼,

private MethodInfo _onStartMethod; 

您可能對上述字段感興趣,它在此方法中設置,如果您花幾分鍾使用此代碼,您會發現 MVC 正在做的是掃描 global.ascx 文件中存在的方法(實際上是一個繼承 HttpApplication 類的類)當然使用反射

此外,如果您觀察堆棧跟蹤,您會發現對方法RegisterEventSubscriptionsWithIIS的調用,我猜這就是您問題的答案:“事件從何而來”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM