2

If an asp.net static variable eg.

public static string test;

that exists in 2 different web application that share the same application pool, share also the value of their static variables?

if I set the test var in site 1: "test="1"" can I read this from site 2?

Mohit S
  • 13,723
  • 6
  • 34
  • 69
sparrows
  • 63
  • 1
  • 7
  • 4
    Even if they did (and they don't), this sounds like the wrong solution to the problem – Marc Gravell Jan 27 '14 at 14:37
  • that's very scary that you'd want to – Jonesopolis Jan 27 '14 at 14:38
  • 1
    in iis you can set up 2 different web site eg localhost.site1 and localhost.site2, when you setup an an application you can chose the application pool eg defaultapplicaitonpool, if you use the same application pool can you share static vars? – sparrows Jan 27 '14 at 14:38
  • 1
    @MarcGravell or the right solution, if they wanted to use static variables as static within each app-domain, and where worried they'd get shared. – Jon Hanna Jan 27 '14 at 14:39
  • @sparrows pretty sure you just asked the same question (in a comment) - the answer is *still* no – Marc Gravell Jan 27 '14 at 14:39
  • 1
    @JonHanna that's fair (and of course, easy to check just by doing it ;p) – Marc Gravell Jan 27 '14 at 14:40

2 Answers2

8

No, they do not. Static is only shared across applications in the same AppDomain.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • what do you mean with app domain? the app domain is a synonimous of application pool? – sparrows Jan 27 '14 at 14:42
  • it is very strange because app pool share the same memory area, i don't know why it is not possible, so if i have two web site with the same static var and one web site change the var content i've to change it in both web site? – sparrows Jan 27 '14 at 14:49
  • @sparrows, yes you've to change it in both. Otherwise most static fields in most ASP.NET applications would have incorrect data set by some other ASP.NET application, unless we forced one application pool per instance. – Jon Hanna Jan 27 '14 at 14:55
  • 1
    what about this http://www.codeproject.com/Articles/242386/VB-NET-MEMORY-MAPPING-ACROSS-DIFFERENT-RUNNING-APP – sparrows Feb 05 '14 at 16:59
  • @sparrows: what do you want to know about that article? – John Saunders Feb 05 '14 at 17:13
  • it talks about sharing memory across two applications so it seems what i'm looking for? – sparrows Feb 06 '14 at 09:51
  • @sparrows: I strongly suggest you forget about that unless you've got a much better reason than you have stated here. That's a lot of very specialized code, difficult to maintain, and you seem to have little reason to do it. Why is it so important for you to share this data? Are you sure there's no other way to achieve your goal than to share the data in memory? – John Saunders Feb 06 '14 at 10:10
  • i need this for performance reason , i' have many web app in the server: eg site1.com site2.us site3.ext .... they all use static variables eg public static list users; when an user change i need to distribute the change to all applications site1,2 .... i don't want to use db and don't want to use a distributed cache server i want to use local memory because it increase performance it is my idea and i will use this approach , i need only to know the best way to do it: cache replication or cache sharing. Is MemoryMappedFile the only whay to share memory in this scenario? – sparrows Feb 06 '14 at 10:20
  • I don't know any reason why memory mapped files would not do what you want, but I'll be surprised if this improves your performance. Among other things, I hope that you have already done performance profiling. If you know for a fact that access to this memory is a problem, then go for it. But if you're just guessing, then you may very well be solving the wrong problem. – John Saunders Feb 06 '14 at 10:22
2

Each server has one or more application pools.

From the application pool, at least one process is started for each asp.net application (and other applications that can run on IIS). One process per asp.net application is the norm, but if you use "web garden" mode, then there can be more than one, each of which behave in isolations (like a mini web-farm, hence "web garden").

Each such process will create an AppDomain, which can host ASP.NET code. It is possible to create other AppDomains on this processes though this isn't often necessary with ASP.NET code (you could for example have your ASP.NET code run some untrusted code in a sandbox AppDomain with reduced privileges, but that's not a common scenario).

Each AppDomain has completely separate copies of all static fields (there's an exception with AppDomain-Neutral assemblies, but ASP.NET only loads mscorlib as AppDomain-neutral).

Therefore static fields are safe to use with ASP.NET, in both normal and web-garden mode.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • 1
    static vars are not safe to use in web garged because they have not a global scope!!! this is my problem i'm finding out a way to keep the performance advantage of local cache like static variables , but i've synchronization problems across web garden flowers, i'm developing a custom distributed cache like appfabric. then i have same static vars in the same server but in different applications and i'm finding out a way to keep them sincronized with the best effort – sparrows Jan 27 '14 at 14:57
  • That would make use of static fields much more fraught than it is. Library code in particular would be much more complicated to write. To communicate between app-domains, use [cross app-domain communication](http://stackoverflow.com/q/314268/400547). That said, the more you keep isolated to a given web application, the greater scalability you'll have, though not all apps need that sort of scalability, I'll grant you. – Jon Hanna Jan 27 '14 at 15:16
  • I would not call static fields "safe". I would want it made clear to readers 1) A static field is used by all users and all requests, so you can wind up with data from one user being seen by another, and 2) Because multiple requests can occur simultaneously, locking is required when using a static field. – John Saunders Jan 27 '14 at 15:30
  • @JohnSaunders true, but at least we only have to reason about one application at a time. Domain-neutral assemblies have to be much more careful about statics. – Jon Hanna Jan 27 '14 at 15:40
  • what about this? http://www.codeproject.com/Articles/242386/VB-NET-MEMORY-MAPPING-ACROSS-DIFFERENT-RUNNING-APP – sparrows Feb 05 '14 at 17:00