问题
I'm using Asp.net
FW 4.6.
I have two applications under the same application pool that needs to read (constatntly) a predefined value which is object to change.
Static variables can't be used beucase differnt 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 is under the same App Pool ?
Something like :
MyAppPool
App1 -> func("royi") = 1
App2 -> var a= func2("royi"); //1
回答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.
来源:https://stackoverflow.com/questions/46441605/shared-variable-across-applications-within-the-same-apppool