Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Application Start event handler manually in constructor

In Global, as an alternative to the application AutoEventWireups, it seems that events are exposed for most of the underlying Application events (BeginRequest, AuthorizeRequest, Error, etc), as well as a set of asynch methods like AddOnBeginRequestAsync etc. However, I cannot find an equivalent event for ApplicationStart!

So my question is, is there anyway to subscribe to the 'same' event that the AutoEventWireup method Application_(On)Start is hooked into?

public class Global : HttpApplication
{
    public Global()
    {
        // I can do this ...
        base.BeginRequest += new EventHandler(Global_BeginRequest);
        // Or even AddOnBeginRequestAsync();

        // But how can I do this?
        base.ApplicationStart += new EventHandler(GlobalApplication_Start);
    }

    protected void Global_BeginRequest(object sender, EventArgs e)
    {
      // ...
    }

    protected void Global_ApplicationStart(object sender, EventArgs e)
    {
      // ...
    }
}

(Out of interest ... is there a way to switch off the AutoEventWireups in Global.asax?. Using the AutoEventWireup = "false" attribute only seems to work on aspx pages)

Edit - it seems that ApplicationStart and ApplicationEnd "are special methods that do not represent HttpApplication events". So I might be barking up the wrong tree entirely.

Edit Re : Why would I need this? Unfortunately, a corporate customer has a framework in place whereby new apps need to inherit their custom HttpApplication class, and FWR, their HttpApplication had already implemented the autowireup Application_(On)Start, meaning that I needed to find another way to override the Framework wireup, so that I can Bootstrap my IoC container and Automapper maps. As per Lloyd's answer, I could also bootstrap in the ctor or Init() as well, although this isn't quite the same. Ultimately I was able to change the corporate framework to allow multiple subscriptions.

like image 938
StuartLC Avatar asked Oct 07 '22 06:10

StuartLC


1 Answers

You could override Init:

public class MyApplication : HttpApplication
{

    public override void Init()
    {
        base.Init();
    }

}

However your constructor could also work just as well.

like image 117
Lloyd Avatar answered Oct 13 '22 09:10

Lloyd