3

I'm using Asp.net FW 4.6.

I have two applications under the same application pool that need to read (constantly) a predefined value which is object to change.

Static variables can't be used because different apps (even under same app pool) - won't be shared.

DB/File/Server are known options.

But :

Question :

Is there any code that I can write that can store in mem value which will be accessible through multiple applications which are under the same App Pool ?

Something like :

MyAppPool  

    App1   -> func("royi") = 1
    App2   -> var a= func2("royi"); //1
achecopar
  • 421
  • 1
  • 11
  • 20
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

1 Answers1

1

All ways of interprocess communication, such as Memory Mapped Files and Named Pipes can also be used for sharing data across applications within single AppPool, see for example Sharing memory between two applications.

Because all apps in the same AppPool resides in the same process and are separated just by AppDomain boundaries, you have some additional options:

  • Use MarshalByRefObject to access object instance across AppDomain boundaries. There are already available wrappers that uses MarshalByRefObject to implement cross-AppDomain singleton, for example Stephen Cleary's CrossDomainSingleton or this one.
  • Use pointers and unsafe code to directly access memory allocated by Marshal.AllocHGlobal, especially if speed is your concern. But you will have to use one of the aforementioned methods to share memory address of allocated memory between applications.
Ňuf
  • 6,027
  • 2
  • 23
  • 26
  • The MarshalByRefObject implementations you mentioned are for applications with several appDomains not for different applications in the same app pool. I spent several hours trying to make it work and could never see the other AppDomains. – achecopar Apr 24 '20 at 19:34
  • @achecopar *IIS* uses *AppDomains* to isolate individual applications (within an *AppPool*) from each other. Or at least it is how it worked on *IIS 7.5* years ago, when I successfully used [this](https://learn.microsoft.com/en-us/archive/blogs/jackg/enumerating-appdomains) technique to enumerate *AppDomains* belonging to other applications and *MarshalByRefObject* for communication between these *AppDomains*. But things might have changed since then. – Ňuf Apr 25 '20 at 04:54