How can I register handlers for HttpApplication events outside of Init()

661 views Asked by At

I'm creating a library that needs to run some code at various stages of the ASP.NET request lifecycle.

It's easy to do this when writing a web application by overriding HttpApplication.Init() and registering various handlers there.

However, I want to do this from my library during Application_Start(); I don't want to consumer to have to override Init() and call my event setup code from there.

Is there any way to achieve this?

Here's my ideal setup:

// in the consumer's code
protected void Application_Start()
{
    // should ideally be able to register lifecycle event handlers from the line below
    MyLibrary.Configure(...); 
}
1

There are 1 answers

3
iamkrillin On

You should just be able to do

global.asax

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        MyLib.Configure(this);
    }
}

This here, refers to the application and there are plenty of events you can subscribe to on it including request life cycle events