asp.net - Handling application-wide events without Global.asax -
my project has no "global.asax" various reasons , can't change (it's component). also, have no access web.config, httpmodule not option.
is there way handle application-wide events, "beginrequest" in case?
i tried , didn't work, can explain why? seems bug:
httpcontext.current.applicationinstance.beginrequest += mystaticmethod;
no, not bug. event handlers can bound httpapplication
events during ihttpmodule
initialization , you're trying add somewhere in page_init
(my assumption).
so need register http module desired event handlers dynamically. if you're under .net 4 there news - there preapplicationstartmethodattribute
attribute (a reference: three hidden extensibility gems in asp.net 4):
this new attribute allows have code run way in asp.net pipeline application starts up. mean way early, before
application_start
.
so things left pretty simple: need create own http module event handlers want, module initializer , attribute assemblyinfo.cs
file . here module example:
public class mymodule : ihttpmodule { public void init(httpapplication context) { context.beginrequest += new eventhandler(context_beginrequest); } public void dispose() { } void context_beginrequest(object sender, eventargs e) { } }
to register module dynamically use dynamicmoduleutility.registermodule
method microsoft.web.infrastructure.dll
assembly:
public class initializer { public static void initialize() { dynamicmoduleutility.registermodule(typeof(mymodule)); } }
the thing left add necessary attribute assemblyinfo.cs
:
[assembly: preapplicationstartmethod(typeof(initializer), "initialize")]
Comments
Post a Comment