c# - Why is there no way to unbind an HttpApplication event handler out of IHttpModule initialization scope -


let's want execute action once or couple of times after web application has started , during web request.

public class webapp : httpapplication {     public override void init()     {         base.init();          this.beginrequest += new eventhandler(this.onfirstbeginrequest);     }      private void onfirstbeginrequest(object sender, eventargs e)     {         // action , if ok, unbind handler,         // because need executed once @ first web request         this.beginrequest -= new eventhandler(this.onfirstbeginrequest);     } } 

the following exception thrown:

event handlers can bound httpapplication events during ihttpmodule initialization.

it wouldn't make sense use event handlers in httpapplication instance execute code on first request application, because each time new httpapplication instance created it's going rebind events , code in event handlers run again.

multiple httpapplication instances created asp.net worker process. pooled performance purposes, there can more 1 instance of httpapplication servicing requests web app.

here's pretty article on subject.


Comments