I assume that you are referencing the Global.asax file in an ASP.NET MVC application. Note that your global.asax extends System.Web.HttpApplication, for example:
public class MvcApplication : System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) {
Basically, multiple HttpApplication instances are created to serve multiple incoming HTTP requests . Upon completion of the request, the HttpApplication instance is returned to the pool, which will be reused again, similar to the database connection pool.
You cannot predict how many HttpApplication instances will be created, basically the ASP.NET workflow will create as many as you need to satisfy the HTTP requests that hit your web application. Your Application_Init () gets called twice because two instances of HttpApplication are created, although it just launches your site. Perhaps you have links to other server resources in your HTML file (JavaScript, CSS files, etc.) Or, perhaps, an Ajax request.
If you want to ensure that the code runs only once, then put it in the Application_Start () method in your Global.asax file. Or use bootstrapper
source share