4

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(...); 
}
Vitor Canova
  • 3,918
  • 5
  • 31
  • 55
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
  • IMO when your library starts (I _suppose_ it's a control library)...HttpApplication.Init() has passed long time before...then they have to do it – Adriano Repetti Jan 22 '14 at 16:34
  • Can your library not expose various modules (that implement `IHttpModule`) that the user can just add to configuration? That's the usual way that libraries hook into the pipeline, and each module is `Init`ed – Damien_The_Unbeliever Jan 22 '14 at 17:46
  • @Damien_The_Unbeliever: can you answer with an example? In particular, how can my library configure these modules (this hooking into the lifecycle is just one of the things the library does so just making the whole library a module isn't sufficient). – ChaseMedallion Jan 22 '14 at 18:15
  • It's hard to provide examples because I don't know exactly what you're trying to do. It's easy for a library to contain modules and handlers and configuration handlers so that the consumer just has to add stuff (possibly just copy and paste) into `web.config`. If those kind of extension points don't fit what your library is trying to do, then either your library is trying too hard and really wants to be the application or you need to explain further where/how your library fits into other applications. – Damien_The_Unbeliever Jan 22 '14 at 18:19
  • @Damien_The_Unbeliever: what I'm trying to avoid is having a library that has N things for the consumer to do to set it up. I'd like to have a single Configure() call in application start that does everything. – ChaseMedallion Jan 22 '14 at 18:21
  • Not knowing what your library is trying to achieve, I can't add much further. But it reads like it's trying to do too many things via a single API call, if it's got to plumb into the pipeline and do lots of other (unspecified) things. Again, not knowing the specifics, I'd recommend re-examining the way the entire thing hangs together and try to modularize it (and its dependencies) – Damien_The_Unbeliever Jan 22 '14 at 18:34

1 Answers1

0

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

iamkrillin
  • 6,798
  • 1
  • 24
  • 51
  • Won't this run into issues when multiple HttpApplication instances are used (see http://stackoverflow.com/questions/6697409/how-many-instances-of-an-application-object-can-run-per-application)? – ChaseMedallion Jan 22 '14 at 18:16
  • You should be fine, even if you have multiple instances of the same app running in the same app pool, they would be running under isolated app domains. – iamkrillin Jan 22 '14 at 18:25
  • That seems to disagree with the answer to the SO question I posted. It says that HttpApplication instances are reused where possible but that multiple are required to handle multiple requests simultaneously – ChaseMedallion Jan 22 '14 at 19:09