0
votes

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?

1
Related, but doesn't answer this question: stackoverflow.com/questions/720969/…Triynko

1 Answers

0
votes

According to your description, first, we need to know below two points:

  1. Application_Start will only be invoked after the first HttpApplication object is created and subsequent HttpApplication instances that are created do not trigger this event.

  2. Each HttpApplication instance will create a new set of HttpModule and call the Init method after it is created.

We can check the original code to see how HttpApplication is created:

enter image description here

We can find above code at System.Web.HttpApplicationFactory.GetNormalApplicationInstance

So in single AppDomain, if there are not enough instances of HttpApplication when concurrent access occurs, new HttpApplication instances will be created and HttpApplication.Init will be called after every instance created. But Application_Start only be called once after the first HttpApplication instance created.

According to the above description, i think you should set up the listener in the Application_Start event. When horizontal scale happens, Application_Start will be called every time after the new AppDomain is created.