Scope of static variables in ASP.NET sites

后端 未结 3 496
南方客
南方客 2020-12-31 09:25

If running multiple ASP.NET applications in the same application pool, how many instances will I have of a class\' static variable?

  1. One per application pool?
3条回答
  •  误落风尘
    2020-12-31 10:21

    Based on the fact that there will be one instance of a static variable per AppDomain, and regarding this (almost 10 year old) article by K. Scott Allen, there is one AppDomain per ASP.NET application, I will conclude that there will be one instance of each shared variable per ASP.NET Web application, even though they all run in the same application pool.

    If introducing more worker processes, I would suspect this to be one instance per application per process it's running in.

    Even though the code for both of the applications resides inside the same process, the unit of isolation is the .NET AppDomain. If there are classes with shared or static members, and those classes exist in both applications, each AppDomain will have it’s own copy of the static fields – the data is not shared.

    (http://odetocode.com/Articles/305.aspx, see the section "AppDomains and you").

    So, the answer to my original question would be 3), if running one worker process.

提交回复
热议问题