1

I looked for an answer to this for quite sometime now but didn't find any.

I will refer to Application start as a stage.

Most of the documentations out there talk about the Global.asax file and the Application_Start method which is invoked only once when the first request reaches the Application, even if this behaviour is similar to subscribing to an event it's technicaly not an event.

While lifecycle events such as BeginRequest, AuthenticateRequest and so on are accessible through the application instance as events, the Application.Start is not.

I can subscribe to BeginRequest event in an HttpModule.Init() method or Global.asax Init() method but not to the Application.Start stage like so:

Module

public class MyModule : IHttpModule
{
  public void Init(HttpApplication httpApplication)
  {
    httpApplication.BeginRequest += new EventHandler(ApplicationBeginRequest);
  }
}

Global

public class Global : HttpApplication
{
  public override void Init()
  {
    BeginRequest += new EventHandler(ApplicationBeginRequest);
  }
}

My question:

Since there is no HttpApplication.Start event accessible from the Application instance, is Global.asax and the "Application_Start" method the only hope to subscribe to the Application start stage ?

Samir
  • 283
  • 2
  • 10
  • Check this out: https://stackoverflow.com/questions/3119034/how-to-handle-application-start-event-in-asp-net-module – Wiktor Zychla Feb 12 '19 at 13:01
  • @WiktorZychla thanks for the link but my question is specifically about Application_Start – Samir Feb 12 '19 at 13:07
  • Adding the Event has to be done before the Request is started. So putting code in the Init method is the right place to add the event. – jdweng Feb 12 '19 at 13:12
  • Refer to this link, https://odetocode.com/articles/89.aspx – Vishal Feb 12 '19 at 13:22
  • @jdweng Using Init method for code that i want to be executed once in the Application lifecycle is a bad option as the Init method is called once for every instance of the HttpApplication. – Samir Feb 13 '19 at 09:12
  • 1
    You need to register the event once for each instance. All instances can call the same method. When instances share the same method you may need add a lock to prevent collisions. – jdweng Feb 13 '19 at 10:01
  • @jdweng But that's not what im trying to do though, i want my code to be executed only once in the hole application lifecycle ! – Samir Feb 13 '19 at 12:28
  • You said you have "multiple instances" so you absolutely need to register the event for each instance you create. – jdweng Feb 13 '19 at 12:33
  • @jdweng Im not refering to HttpApplication (multiple instances per ApplicationDomain) as Application (one application per ApplicationDomain). The ApplicationManager handles the creation of HttpApplication instances for me and i want my code to be executed once for all the instances. check out this link https://learn.microsoft.com/en-us/previous-versions/ms178473%28v%3dvs.140%29 – Samir Feb 13 '19 at 14:34
  • What was confusing is in your global you have : BeginRequest += new EventHandler(ApplicationBeginRequest); Why is this needed? You are writing Server code. The server should be only one instance of the application. You will have multiple instances of the socket class (one for each client). You will have one Listener. To have one application receive message have to be sent from the socket layer up to the application layer. – jdweng Feb 13 '19 at 15:38

1 Answers1

0

After jumping to the .NET source code i found that the HttpApplicationFactory class looks for a method named Application_OnStart or Application_Start in the Global.asax file and then invokes it using reflection => ReflectOnMethodInfoIfItLooksLikeEventHandler().

I don't have the answer why HttpApplication.Start is not event but it's clearly intended to be handled in an event like fashion using Application_OnStart or Application_Start methods.

Samir
  • 283
  • 2
  • 10