简体   繁体   中英

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

I have an ASP.Net MVC application. Inside there is the famous Global.asax (I didn't touch it):

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

Now, people said this is the event which is being fired when the application starts. I'm sorry but what I see here is not an event - This is a method. I believe there is an event somewhere that is being fired, and is pointing on this METHOD (so this method is an event handler. Am I wrong?)

My Q is where is the event itself? I looked for it in all the solution and didn't find it.

Well after getting the stack trace the leads to the call to Application_Start,I found it was like

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)

This link contains the source code for the whole thing,

private MethodInfo _onStartMethod; 

You are probably interested in the above field, it gets set in this method , if u would spend a couple of minutes with this code u will find that what MVC is doing is scanning for methods existing in the global.ascx file (which is actually a class inheriting HttpApplication class) using reflection of course

Also if you observed the stack trace , u will find a call to the method RegisterEventSubscriptionsWithIIS , which I am guessing is the answer to your question:"where does events come from"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM