Preventing code being loaded to another AppDomain
Asked Answered
H

1

8

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?

Hydromedusa answered 3/3, 2014 at 9:49 Comment(10)
hi dejan is it web app or win based solution.Chagres
Dude, seriously? If the question itself, containing lots of talk about Excel, absolutely no reference to anything remotely connected to web or question tags don't give it away, here it is: It's "win based solution". We have networking components, but I don't think it's relevant for this problem at all.Hydromedusa
have to ask, even you are using c# .net the approach changes for solution to solution :)Chagres
How are you inspecting the app domains? Are you calling load on the 2nd app domain. Why do you need 2 app domains (is the second App domain created and then marshalled from the first - being ignorant to the structure of the plugin) Is it a VSTO project you are using? You talk about gateways and not loading code twice, why? A bit more context may help.Scotch
First app domain is created by Excel (Default Domain), second app domain is created by a plug-in that's loaded before ours (it's third party plugin, we don't have control over it - meaning, we don't need it - we don't want it even :D ). Our project is not VSTO, it's a combination of COM and Automation addins. "gateway" is just an expression I use to express the phenomenon of passing execution of our code from one domain to another, those are the places in our automation from which we can see our code starting to execute on another domain (it is the example I gave with the OnTime function)Hydromedusa
Please feel free to ask any other questions, this is not an easy question at all, and I don't know if I missed out on anything, but by now I think there is actually no way to prevent your code from being loaded by another addin in a case like this. There are three sides to this story, and one side (third party plugin I mentioned), is not being respectful to others (poor us :( ) by forcing load time of our components, so please if you are making plugins for excel, and you need something loaded, don't tell all installed addins to load, just load your own!Hydromedusa
If these are COM addins, do they have a COM prog. ID? (e.g. is there an attribute on your c# class from System.Runtime.InteropServices where you set ComVisible to true and set a string ProgId?)Largo
Yeah, our COM addin has an ID, it's not specifically set, we use GUID for our ComVisible addins. (I think you have to have an ID, that's why it is called like that)Hydromedusa
You can use a mutex to ensure that only one instance of the app is loaded and running, something like this: mikeperetz.blogspot.co.uk/2013/12/…Orangutan
What if someone other than me loaded it and ran it the first time, do I lock myself out? The thing is their COM addin forces us to load, loading twice is only one part of the problem, update doesn't explicitly say it, but we managed to work around that part of the problem...Hydromedusa
G
2

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

Garibald answered 12/3, 2014 at 12:14 Comment(1)
Please take a look at comments for details, we found a way to don't have our code loaded twice in two app domains (that's what an update of question is supposed to say). Solution I will accept is gonna prevent our code from being loaded, because having one instance is not helping us if we are running from third party domain (for some reason, we have a synchronization context problems). Please correct me if I'm wrong, but I think this answer is not gonna prevent code from being loaded to other app domains in the first place.Hydromedusa

© 2022 - 2024 — McMap. All rights reserved.