Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3331598
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:36:02+00:00 2026-05-17T23:36:02+00:00

I have an Excel plug-in (written in C#) with a static variable that is

  • 0

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:

  1. Event handlers on a VSTO ribbon-bar initialize the instance, and also read it for display in helper dialogs
  2. An RTD server (a class that is declared [ComVisible] and implements the IRtdServer interface) utilizes the data for RTD formulas
  3. 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:36:02+00:00Added an answer on May 17, 2026 at 11:36 pm

    Your static variable is certainly not shared between AppDomains, so what you are seeing is as expected, given the different AppDomains.

    I think it works like this:

    The VSTO add-in runs in its own AppDomain. If the COM class factory for your cache object (or RTD server) is created from within that AppDomain, it will be loaded into the calling AppDomain. Subsequent access to that COM class will find it already loaded into the process, and use the existing instance.

    However, if the first activation is triggered by Excel itself, e.g. by an RTD call, the .NET implemented COM object will be loaded into the default AppDomain of the process. You have no control over this part of the loading process unless you make an unmanaged shim, since ‘your code’ is not running when the loading happens.

    Some suggestions off the top of my head:

    1. Make some wrapper functions for the RTD calls which are exposed from your .NET add-in. This way, you can ensure the RTD class is loaded before calling Excel’s Application.RTD to do the real RTD setup.

    2. Make the access from the RTD server to the actual cache through user-defined functions – this way Excel will call into the AppDomain that has the real cache, even if it is not the current AppDomain where the RTD server lives.

    3. Try to get hold of the Add-In object via Application.AddIns…. there is a way to get the actual add-in COM object, and use some interface on it to get to the cache…

    4. Make an unmanaged shim (search the web for “COM shim wizard”) for your RTD server. Somehow figure out how to get your VSTO AppDomain loaded, and then load the RTD server into that AppDomain.

    5. See if there is a way to get the VSTO add-in loaded into the default AppDomain. I don’t know, but perhaps there is a flag or switch that tells the “Microsoft Office Systems loader” (or whatever that part is called now) not to create the isolated AppDomain.

    6. The right answer: use Excel-Dna (disclaimer: I’m the developer). It supports Ribbons, RTD and UDFs in your managed add-in, with no registration needed, and everything gets put into your add-ins AppDomain. It’s free, but it will take some time and effort to port your stuff – RTD is trivial, but if you use a lot of the VSTO helper objects for the Ribbon and sheet access (tables etc.), you’ll need to think about it a bit.

    I hope this gives you some ideas.

    –Govert–

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Excel Spreadsheet like this id | data for id | more
I have Excel add-in which I add so many class modules that it is
We have an Excel 2002/XP based application that interacts with SQL 2000/5 to process
I have an Excel 2003 workbook that contains a macro to copy certain of
I have an excel sheet full of times. They are formatted so that they
I have a macro that needs to run for update. If Excel is in
I have an excel sheet that is loaded with a dynamic result set of
I have an Excel spreadsheet that I'm reading in that contains some £ signs.
I have an excel spreadsheet that contains some formulas to calculate ROI for an
I have an excel spreadsheet in a format similar to the following... | NAME

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.