The terminology is a little unclear around horizontal scaling in Azure.
When it creates multiple instances of an application, I understand that these run in separate VMs, so when such horizontal scaling happens, this should cause entirely new AppDomains with their own set of static variables to be created.
If that's the case, then is it true that Application_Start event would be called for each instance?
We have a "cache flush" feature that involves setting up a listener to subscribe to a "topic" in a message queue, so that it will flush a static cache when a message is received. We used to think we had to set up the listener in the HttpApplication.Init event, which would be called for each instance, but that no longer makes sense, after realizing that all HttpApplication instances within the AppDomain share the same set of static variables.
My new understanding is that multiple HttpApplication instances (and therefore multiple calls to HttpApplication.Init) will occur in a web app even without horizontal scaling. In other words, it's a normal feature of how an asp.net web app handles requests even in a single AppDomain. Azure's horizontal scaling is completely different, and involves instantiating entirely separate AppDomains. Does that sound right?
According to your description, first, we need to know below two points:
Application_Startwill only be invoked after the firstHttpApplicationobject is created and subsequentHttpApplicationinstances that are created do not trigger this event.Each
HttpApplicationinstance will create a new set ofHttpModuleand call the Init method after it is created.We can check the original code to see how
HttpApplicationis created:We can find above code at
System.Web.HttpApplicationFactory.GetNormalApplicationInstanceSo in single AppDomain, if there are not enough instances of
HttpApplicationwhen concurrent access occurs, newHttpApplicationinstances will be created andHttpApplication.Initwill be called after every instance created. ButApplication_Startonly be called once after the firstHttpApplicationinstance created.According to the above description, i think you should set up the listener in the
Application_Startevent. When horizontal scale happens,Application_Startwill be called every time after the new AppDomain is created.