I have an Excel plug-in (written in C#) with a static variable that is at the heart of a singleton data cache:
static DataCache _instance;
This is accessed via three different code paths:
- Event handlers on a VSTO ribbon-bar initialize the instance, and also read it for display in helper dialogs
- An RTD server (a class that is declared [ComVisible] and implements the IRtdServer interface) utilizes the data for RTD formulas
- A set of automation calls (implemented in another class that is declared [ComVisible]) also operate on the data. These are called by way of VBA code that is invoked when buttons on the Excel worksheet are clicked.
EDIT (#3):
Depending on the order in which these code paths are first invoked, I find that my code runs in two separate AppDomains.
All access from the ribbon-bar event handlers occurs in an AppDomain called "MyPlugIn.vsto". If this is the FIRST access to my COM object, then all subsequent calls (including RTD calls) occur in the same AppDomain.
However if the FIRST access is via the RTD interface, then that call and all subsequent RTD calls occur in an AppDomain called "DefaultDomain". (This happens when loading a saved document with embedded RTD formulas.) Subsequent calls to initialize and manipulate the DataCache via the toolbar still occur in the "MyPlugIn.vsto" AppDomain. This means that the RTD formulas always run as if the DataCache were not initialized (since the static variable set in one AppDomain remains uninitialized in the other).
It appears that Excel or the VSTO is creating an AppDomain when VSTO initializes. Objects created via COM interop before this initialization land in the default AppDomain, while objects created afterwards land in the VSTO AppDomain.
How can I ensure that the same DataCache instance is utilized, no matter which AppDomain my RTD server object gets created in?