Skip to content

Instantly share code, notes, and snippets.

@DinisCruz
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DinisCruz/8787331 to your computer and use it in GitHub Desktop.
Save DinisCruz/8787331 to your computer and use it in GitHub Desktop.
Get current HttpHandlers for WebApplication
var eventHandlerList = (EventHandlerList)HttpContextFactory.Context.ApplicationInstance.prop("Events");
var eventList = new List<Object>();
var next = eventHandlerList.field("head");
while (next != null)
{
eventList.add(next.field("key"));
next = next.field("next");
}
return eventList; // there are 12 of these, and they are all objects
//using System.ComponentModel;
var application = HttpContextFactory.Context.ApplicationInstance;
var isContainerInitalizationAllowed = application.type().BaseType
.BaseType
.properties().where(prop=>prop.Name =="IsContainerInitalizationAllowed").first();
return PublicDI.reflection.getProperty(isContainerInitalizationAllowed, application);
// the value was false (in IIS)
var application = HttpContextFactory.Context.ApplicationInstance;
var asyncEventsProp = application.type().BaseType.BaseType.properties().where(prop=>prop.Name == "AsyncEvents").first();
var asyncEvents = PublicDI.reflection.getProperty(asyncEventsProp, application);
return asyncEvents.typeFullName();
// type returned is System.Web.HttpApplication+AsyncAppEventHandlersTable
var application = HttpContextFactory.Context.ApplicationInstance;
var asyncEventsProp = application.type().BaseType.BaseType.properties().where(prop=>prop.Name == "AsyncEvents").first();
var asyncEvents = PublicDI.reflection.getProperty(asyncEventsProp, application);
var hashTable = (Hashtable)asyncEvents.field("_table");
return hashTable;
var application = HttpContextFactory.Context.ApplicationInstance;
var eventHandlerList = (EventHandlerList)application.prop("Events");
var requestHandler = typeof(HttpApplication).fieldValue("EventBeginRequest");
//option 1: directly
//return eventHandlerList[requestHandler];
//option 2: using reflection
return eventHandlerList.invoke("Find",requestHandler)
.field("handler").typeFullName();
//using System.Web;
var application = HttpContextFactory.Context.ApplicationInstance;
var eventHandlerList = (EventHandlerList)application.prop("Events");
var requestHandler = typeof(HttpApplication).fieldValue("EventBeginRequest");
var eventHandler = eventHandlerList[requestHandler];
return eventHandler.invoke("GetMethodImpl"); // signature of EventBeginRequest delegates
return eventHandler.GetInvocationList(); // current implementations
return eventHandler.Method; // also gets the MethodInfo object (the signature)
return eventHandler.Target; // gets the Target objet (for example the global.asax object)
return eventHandler.invoke("GetTarget"); // also gets the target (using reflection)
var application = HttpContextFactory.Context.ApplicationInstance;
var eventHandlerList = (EventHandlerList)application.prop("Events");
// this returns a list of all fields
return typeof(HttpApplication).fields().select(field => field.Name ).asString() ;
//this only gets the fields that start with Event:
return typeof(HttpApplication).fields().where(field=>field.Name.starts("Event"))
.select(field => field.Name )
.asString() ;
//this gets the objects of those event fields:
var eventsFields = typeof(HttpApplication).fields().where(field=>field.Name.starts("Event"))
.select(field => typeof(HttpApplication).fieldValue(field.Name))
.asString() ;
return eventsFields;
//Better version where the events's objects are mapped by name:
var eventsFields = new Dictionary<String,Object>();
foreach(var field in typeof(HttpApplication).fields().where(field=>field.Name.starts("Event")))
eventsFields.Add(field.Name, typeof(HttpApplication).fieldValue(field.Name));
return eventsFields;
//Mapping the current EventHandlerList.ListEntry
var eventsFields = new Dictionary<String,Object>();
foreach(var field in typeof(HttpApplication).fields().where(field=>field.Name.starts("Event")))
{
var eventObject = typeof(HttpApplication).fieldValue(field.Name);
var listEntry = eventHandlerList.invoke("Find",eventObject); // returns EventHandlerList.ListEntry
// if (listEntry != null)
eventsFields.Add(field.Name, listEntry);
}
return eventsFields;
/* without the commented out null check, the above example returned
{"EventDisposed":null,"EventErrorRecorded":{},"EventRequestCompleted":null,"EventPreSendRequestHeaders":null,
"EventPreSendRequestContent":null,"EventBeginRequest":{},"EventAuthenticateRequest":{},"EventDefaultAuthentication":null,
"EventPostAuthenticateRequest":{},"EventAuthorizeRequest":{},"EventPostAuthorizeRequest":null,"EventResolveRequestCache":{},
"EventPostResolveRequestCache":{},"EventMapRequestHandler":null,"EventPostMapRequestHandler":null,
"EventAcquireRequestState":{},"EventPostAcquireRequestState":{},"EventPreRequestHandlerExecute":null,
"EventPostRequestHandlerExecute":null,"EventReleaseRequestState":{},"EventPostReleaseRequestState":null,
"EventUpdateRequestCache":{},"EventPostUpdateRequestCache":null,"EventLogRequest":null,"EventPostLogRequest":null,
"EventEndRequest":{}}
and this with the listEntry null check (i.e. only the current 12 that are being executed will be shown)
{"EventErrorRecorded":{},"EventBeginRequest":{},"EventAuthenticateRequest":{},"EventPostAuthenticateRequest":{},
"EventAuthorizeRequest":{},"EventResolveRequestCache":{},
"EventPostResolveRequestCache":{},"EventAcquireRequestState":{},
"EventPostAcquireRequestState":{},"EventReleaseRequestState":{},
"EventUpdateRequestCache":{},"EventEndRequest":{}}
*/
// here is the list current events and who they are mapped to:
var eventsFields = new Dictionary<String,Object>();
foreach(var field in typeof(HttpApplication).fields().where(field=>field.Name.starts("Event")))
{
var eventObject = typeof(HttpApplication).fieldValue(field.Name);
var listEntry = eventHandlerList.invoke("Find",eventObject); // returns EventHandlerList.ListEntry
if (listEntry != null)
{
var handler = listEntry.field("handler");
var target = handler.prop("Target");
var method = handler.prop("Method");
eventsFields.Add(field.Name, "{0} - {1}".format(target,method));
}
}
return eventsFields;
// and finally here is a better version that uses the Invocation list to get all methods invoked (not just the first one like in the example above)
var application = HttpContextFactory.Context.ApplicationInstance;
var eventHandlerList = (EventHandlerList)application.prop("Events");
var eventsFields = new Dictionary<String,Object>();
foreach(var field in typeof(HttpApplication).fields().where(field=>field.Name.starts("Event")))
{
var eventObject = typeof(HttpApplication).fieldValue(field.Name);
var listEntry = eventHandlerList.invoke("Find",eventObject); // returns EventHandlerList.ListEntry
if (listEntry != null)
{
var handler = listEntry.field("handler");
var invocationList = (Delegate[])handler.invoke("GetInvocationList");
var eventMapping = "";
for(int i =0 ; i < invocationList.size() ; i++)
eventMapping += "[{0} - {1} : {2}] ".format(i, invocationList[i].Method, invocationList[i].Target);
eventsFields.Add(field.Name, eventMapping);
}
}
return eventsFields;
// which produces this output:
/*
{"EventErrorRecorded":" [0 - Void Application_Error(System.Object, System.EventArgs) : ASP.global_asax] ",
"EventBeginRequest":" [0 - Void Application_BeginRequest(System.Object, System.EventArgs) : ASP.global_asax] ",
"EventAuthenticateRequest":" [0 - Void OnEnter(System.Object, System.EventArgs) : System.Web.Security.WindowsAuthenticationModule]
[1 - Void AuthenticateRequestHandler(System.Object, System.EventArgs) : System.Web.Handlers.ScriptModule]
[2 - Void Application_AuthenticateRequest(System.Object, System.EventArgs) : ASP.global_asax] ",
"EventPostAuthenticateRequest":" [0 - Void OnEnter(System.Object, System.EventArgs) : System.Web.Security.DefaultAuthenticationModule] ",
"EventAuthorizeRequest":" [0 - Void OnEnter(System.Object, System.EventArgs) : System.Web.Security.UrlAuthorizationModule]
[1 - Void OnEnter(System.Object, System.EventArgs) : System.Web.Security.FileAuthorizationModule] ",
"EventResolveRequestCache":" [0 - Void OnEnter(System.Object, System.EventArgs) : System.Web.Caching.OutputCacheModule] ",
"EventPostResolveRequestCache":" [0 - Void OnApplicationPostResolveRequestCache(System.Object, System.EventArgs) : System.Web.Routing.UrlRoutingModule] ",
"EventAcquireRequestState":" [0 - Void OnEnter(System.Object, System.EventArgs) : System.Web.Profile.ProfileModule]
[1 - Void Application_AcquireRequestState(System.Object, System.EventArgs) : ASP.global_asax] ",
"EventPostAcquireRequestState":" [0 - Void OnPostAcquireRequestState(System.Object, System.EventArgs) : System.Web.Handlers.ScriptModule] ",
"EventReleaseRequestState":" [0 - Void OnReleaseState(System.Object, System.EventArgs) : System.Web.SessionState.SessionStateModule] ",
"EventUpdateRequestCache":" [0 - Void OnLeave(System.Object, System.EventArgs) : System.Web.Caching.OutputCacheModule] ",
"EventEndRequest":" [0 - Void OnEndRequest(System.Object, System.EventArgs) : System.Web.SessionState.SessionStateModule]
[1 - Void OnLeave(System.Object, System.EventArgs) : System.Web.Profile.ProfileModule]
[2 - Void EndRequestHandler(System.Object, System.EventArgs) : System.Web.Handlers.ScriptModule] "}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment