Home » excel » c# – Preventing code being loaded to another AppDomain

c# – Preventing code being loaded to another AppDomain

Posted by: admin March 9, 2020 Leave a comment

Questions:

We have a plugin for excel that is normally working fine, and we don’t have anything set up for using AppDomains.

However, some other plugin seems to force our code to load the second time, and we end up having our code running on two different AppDomains (when we want to execute an action, we will put it in a queue on AppDomain 1, and then, the Excels OnTime function runs dequeue method on AppDomain 2, where the queue is empty).

So, my question is this: is there a simple way to get around this problem, maybe prevent our program from being loaded twice or by other AppDomains? Does anyone have any other idea on how to approach this issue?

Update:

What we managed to do, was to load our whole program into other plugin’s AppDomain when we are called, and now we have strangest problem at the same places (places that used to be “gateways to other domains”, like the mentioned action queue function):

The SynchronizationContext.Current seems to reset to null whenever there is such case, for example for every call of the click handler on ribbon, the SynchronizationContext.Current is set to null.

I even tried debugging the .Net code, and it happens that framework remembers an old synchronization context, and when getting everything back to how things were, old synchronization context is null.

Can anyone give any help on this issue, I’m really starting to lose it?

How to&Answers:

I think this could be a solution for your problem:

http://ingebrigtsen.info/2007/05/18/cross-appdomain-singleton/

and an update to the post above:

http://web.archive.org/web/20120427003305/http://www.dolittle.com/blogs/einar/archive/2007/05/30/crossappdomainsingleton-update.aspx