在VS2008 / Casini中启动调试时,为什么Application_Init会触发两次? [英] Why would Application_Init fire twice when starting debugging in VS2008/Casini?

查看:105
本文介绍了在VS2008 / Casini中启动调试时,为什么Application_Init会触发两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在VS2008 / Casini中启动调试时,Application_Init为什么会触发两次?

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

是的,它发生在global.asax中。

Yeah, It's happening in global.asax. Seems fairly random though, only happens once in a while.

推荐答案

我假设您要引用的是Global.asax文件。一个ASP.NET MVC应用程序。请注意,您的global.asax扩展了System.Web.HttpApplication,例如:

I assume you're referring the the Global.asax file in an ASP.NET MVC application. Notice that your global.asax extends System.Web.HttpApplication eg:

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);
    }
}

基本上正在实例化多个HttpApplication实例,以服务多个传入的HTTP请求。请求完成后,HttpApplication实例将返回到一个池中以再次使用,类似于数据库连接池。

Basically multiple HttpApplication instances are being instantiated to serve multiple incoming HTTP requests. Once the request is finished, the HttpApplication instance goes back into a pool to be reused again, similar to database connection pooling.

您无法预测将有多少个HttpApplication实例。创建后,基本上ASP.NET辅助进程将创建所需数量的内容来满足击中Web应用程序的HTTP请求的需求。您的Application_Init()被调用两次,因为正在创建2个HttpApplication实例,即使您只是在运行网站。可能是您引用了HTML中其他服务器端资源(JavaScript文件,CSS等),也可能是Ajax请求。

You can't predict how many HttpApplication instances will be created, basically the ASP.NET worker process will create as many as it needs to fulfill demand from HTTP requests hitting your web app. Your Application_Init() is getting called twice because 2 HttpApplication instances are being created, even though it's just you running your website. It could be that you have references to other server-side resources in your HTML being pulled in (JavaScript files, CSS etc.), or maybe an Ajax Request.

如果您想保证代码仅运行一次,然后将其放入Global.asax的Application_Start()方法中。 或使用引导程序

If you want to guarantee code is only run once, then put it in the Application_Start() method in your Global.asax. Or use a Bootstrapper

这篇关于在VS2008 / Casini中启动调试时,为什么Application_Init会触发两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆