How Many Times Does an ASP .NET Application Start?
ASP .NET应用程序启动多少次?
I want to run something once per AppDomain (specifically RegisterRoutes). If I put the code I want to execute in the global.asax in Application_Start, everything's good (or so it seems) and the code seems to only execute once.
我想每个AppDomain运行一次(特别是RegisterRoutes)。如果我把我想要在Application_Start中的global.asax中执行的代码放在一起,那么一切都很好(或者看起来如此),代码似乎只执行一次。
But If I have a custom HTTP Module registered in the web.config that does the following:
但是如果我在web.config中注册了一个自定义HTTP模块,它执行以下操作:
public class SomeHttpModule:IHttpModule
{
public void Init(HttpApplication context)
{
new SomeRunner().Run();
}
public void Dispose()
{
}
}
public class SomeRunner
{
private static object syncLock = new object();
private static bool hasRun;
public void Run()
{
lock(syncLock)
{
if (!hasRun)
{
hasRun = true;
RegisterRoutes();
}
}
}
public void RegisterRoutes()
{
// Register MVC Routes
}
}
When I hit "go" in Visual Studio, my debugger stops at my breakpoint at the first line of the Run method...but lo and behlold if I check the RouteTable.Routes collection...the MVC routes have already been registered (meaning RegisterRoutes must have already been called)...even though hasRun is false!
当我在Visual Studio中点击“go”时,我的调试器在Run方法的第一行的断点处停止...但是如果我检查RouteTable.Routes集合那么lo和behlold ...... MVC路由已经注册了(意思是RegisterRoutes必须已被调用)...即使hasRun为false!
Is this some sort of oddness running in Visual Studio debugging an IIS website? I know that IIS can host two HttpApplications in one AppDomain and that would have SomeHttpModule get Init'd twice in the same AppDomain...right? But how would my static bool hasRun still possibly be false???
这是在Visual Studio调试IIS网站时运行的某种奇怪吗?我知道IIS可以在一个AppDomain中托管两个HttpApplications,并且SomeHttpModule会在同一个AppDomain中获得两次Init'd ...对吗?但是我的静态bool怎么会运行仍然可能是假的?
Thanks.
谢谢。
1 个解决方案
#1
1
A web application can be started many times. The application can be closed down whenever IIS thinks that it's not used, and when the next request comes in the application will start again.
Web应用程序可以多次启动。只要IIS认为它没有被使用,就可以关闭应用程序,当应用程序中的下一个请求进入时,应用程序将重新启动。
Also, the application might be set to recycle daily, in that case it would start at least once a day.
此外,应用程序可能设置为每天回收,在这种情况下,它将至少每天启动一次。
I'm not sure why the application is started twice when you are debugging it, but it might have something to do with how the debugger is attached to the process.
我不确定为什么应用程序在调试时会启动两次,但它可能与调试器附加到进程的方式有关。
The reason that the static variable doesn't survive from one instance of the application to the next, is that they are separate instances. When the application starts again, it starts with it's own set of variables. It starts in a completely new virtual address space, so there is nothing left from the previous instance.
静态变量不能从应用程序的一个实例存活到下一个实例的原因是它们是单独的实例。当应用程序再次启动时,它会从它自己的一组变量开始。它从一个全新的虚拟地址空间开始,因此前一个实例没有任何内容。
#1
1
A web application can be started many times. The application can be closed down whenever IIS thinks that it's not used, and when the next request comes in the application will start again.
Web应用程序可以多次启动。只要IIS认为它没有被使用,就可以关闭应用程序,当应用程序中的下一个请求进入时,应用程序将重新启动。
Also, the application might be set to recycle daily, in that case it would start at least once a day.
此外,应用程序可能设置为每天回收,在这种情况下,它将至少每天启动一次。
I'm not sure why the application is started twice when you are debugging it, but it might have something to do with how the debugger is attached to the process.
我不确定为什么应用程序在调试时会启动两次,但它可能与调试器附加到进程的方式有关。
The reason that the static variable doesn't survive from one instance of the application to the next, is that they are separate instances. When the application starts again, it starts with it's own set of variables. It starts in a completely new virtual address space, so there is nothing left from the previous instance.
静态变量不能从应用程序的一个实例存活到下一个实例的原因是它们是单独的实例。当应用程序再次启动时,它会从它自己的一组变量开始。它从一个全新的虚拟地址空间开始,因此前一个实例没有任何内容。