Shared variable across applications within the same AppPool?

二次信任 提交于 2019-12-20 02:58:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!