Why does Application_Init fire twice when starting debugging in VS2008 / Casini?

Why does Application_Init fire twice when starting debugging in VS2008 / Casini?

Yes, this is happening in global.asax. It seems rather random, though, it only happens from time to time.

+4
source share
1 answer

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) { // (snip) } protected void Application_Init() { // Why is this running twice? } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.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

+6
source

Source: https://habr.com/ru/post/1316506/


All Articles