Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared variable across applications within the same AppPool?

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
like image 444
Royi Namir Avatar asked Mar 07 '26 18:03

Royi Namir


1 Answers

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.
like image 171
Ňuf Avatar answered Mar 10 '26 08:03

Ňuf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!