Loading an Add-In

In my last entry, Using the Add-In Manager to Load and Unload Add-Ins , I mentioned the Microsoft.VisualStudio.Tools.Applications.AddInManager.dll assembly and that it contains classes that can be used to load add-ins into a hosting application.  I also promised that I’d write another entry that explained how to do just that.  This time around, we’ll actually get into some of the code.


By default, the classes in the Add-In Manager load add-ins into their own AppDomains, which the Add-In Manager will create automatically.  This can be accomplished easily using a piece of code like this:


AddIn addIn = new AddIn("My Arbitrary AddIn Name", @"c:\myaddins\myaddin.dll");

AddInCollection addInCollection = new AddInCollection();


Context addInContext = new Context("My Arbitrary Context Name", myHostItemProvider);




Let’s examine this example in detail.  First, we instantiate an AddIn that will be used to load the add-in assembly “c:\myaddins\myaddin.dll”.  We give it a name to identify it later, which is also going to be used as the FriendlyName of the AppDomain that will be created for it later on.  Note that at this point we haven’t loaded any code or created an AppDomain; all we’ve done is provided some data about the add-in so that we can load it later.  Next the AddInCollection is instantiated and the AddIn added to the collection.  As I mentioned earlier this step might seem superfluous at the moment, but for the time being it is necessary.  Finally, we create the context, again with an arbitrary name for identification.


We’ll also supply our IHostItemProviderContract implementation to the Context, which will be passed into each add-in when it starts up.  The IHostItemProviderContract is basically the mechanism through which the add-in bootstraps communicates with the hosting application.  Details on implementing IHostItemProviderContract are provided in the VSTA SDK documentation, so I won’t go into that here.


The last method call to AddIn.Load actually does all the work.  The Add-In Manager creates a new fully-trusted AppDomain with the ApplicationBase property set to “c:\ mycodebase.”  Note that the ApplicationBase is passed to the Load function, the path information passed to the AddIn’s constructor is not used here.  Behind the scenes, the Add-In Manager then uses a combination of Assembly.LoadFrom and Assembly.Load to load the add-in passed to the constructor into the new AppDomain.  That sounds a little complicated, but it results in the behavior that one would expect: the path information specified to the constructor will be honored if no assembly with the correct name can be found in the GAC or the ApplicationBase.


Once the add-in assembly has been loaded into the target domain, the Add-In Manager invokes its constructor and starts calling various methods on it to start it up.  This eventually results in a method called InternalStartup being called, and an OnStartup event being fired.  These are the two main entry points to the user’s add-in code.  The user’s code will most likely advise on some events of the object model in this method, or do any other necessary startup work.  Once the initialization code completes, the add-in is in a state that we call “Loaded,” meaning that the AddIn.IsLoaded property will be true.  For the most part, you can think of “Loaded” as meaning “Executing” when it pertains to the Add-In Manager, since there is no concept of a “The assembly is loaded but not yet executing” state.

So, that’s a simple rundown of how the Add-In Manager loads add-ins by default.  Of course, we’ve designed the Add-In Manager so that it also supports loading add-ins into specific AppDomains.  We’ve also built-in numerous other features to support partially-trusted add-ins.  More on that later.

Aaron Hare
Software Design Engineer


Comments (13)

  1. Richard Hein says:

    Great!  I really appreciate all your posts, that’s a ton of information.  I thought I saw a demo a long time ago where they showed VSA in a web application.  Is that possible?

  2. It has been a while since I’ve blogged, and I still haven’t delivered on my promise that I would explain…

  3. news says:

    Hjelp Kontakt oss http://ameriplan-dental.setcap.org , your health <a href=http://ameriplan-dental.setcap.org>ameriplan dental</a>

  4. Novelty says:

    Hjelp Kontakt oss http://ameriplan-dental.setcap.org , your health <a href=http://ameriplan-dental.setcap.org>ameriplan dental</a>

  5. <a href="httpwwwigenqmvhcnpage3html">cofurnituremanufacturingwhalen</a> cofurnituremanufacturingwhalen,<a href="httpwwwigenqmvhcnpage4html">cheyennewyweddingflorists</a> cheyennewyweddingflorists,<a href="httpwwwtyxotwjecnpage98html">myspacelayoutswriters</a> myspacelayoutswriters,<a href="httpwwwigenqmvhcnpage2html">atlascopco1001-b40-10014</a> atlascopco1001-b40-10014,<a href="httpwwwtyxotwjecnpage96html">blackcocksinpinkpussies</a> blackcocksinpinkpussies,<a href="httpwwwigenqmvhcnpage19html">pictureonnellytherapper</a> pictureonnellytherapper,<a href="httpwwwigenqmvhcnpage5html">payerid87726forunitedhealthcare</a> payerid87726forunitedhealthcare,<a href="httpwwwigenqmvhcnpage11html">injectionsitesarcomaoncat</a> injectionsitesarcomaoncat,<a href="httpwwwigenqmvhcnpage11html">misdiagnosisofewingssarcoma</a> misdiagnosisofewingssarcoma,<a href="httpwwwigenqmvhcnpage13html">jasminewimbleyillinois</a> jasminewimbleyillinois,

  6. <a href="httpwwwevzvhqkucnpage4html">yamahamt44</a> yamahamt44,<a href="httpwwwihamuicscnpage92html">ecardsforpalmos</a> ecardsforpalmos,<a href="httpwwwihamuicscnpage91html">changingsettingshistoryviewingandcookies</a> changingsettingshistoryviewingandcookies,<a href="httpwwwihamuicscnpage91html">octobernovemberpfcschoicechangingvisionatthecouncil</a> octobernovemberpfcschoicechangingvisionatthecouncil,<a href="httpwwwevzvhqkucnpage18html">nubilesnet_girls-shannon-dildo_full</a> nubilesnet_girls-shannon-dildo_full,<a href="httpwwwevzvhqkucnpage18html">borokanubiles</a> borokanubiles,<a href="httpwwwihamuicscnpage95html">leverageandcashflow</a> leverageandcashflow,<a href="httpwwwihamuicscnpage97html">weareglitter</a> weareglitter,<a href="httpwwwevzvhqkucnpage15html">snapontruckgrill</a> snapontruckgrill,<a href="httpwwwihamuicscnpage92html">macos101networkinginpearpc</a> macos101networkinginpearpc,

  7. <a href="httpwwwnxedpuuecnpage65html">miamidolphinsblanket</a> miamidolphinsblanket,<a href="httpwwwnxedpuuecnpage51html">checkemailatcharter</a> checkemailatcharter,<a href="httpwwwnxedpuuecnpage78html">hollisrealestatenewhampshire</a> hollisrealestatenewhampshire,<a href="httpwwwnxedpuuecnpage65html">freemsnbackgroundpicturesofdolphins</a> freemsnbackgroundpicturesofdolphins,<a href="httpwwwnxedpuuecnpage73html">shewastopless</a> shewastopless,<a href="httpwwwnxedpuuecnpage65html">ipodnanomiamidolphins</a> ipodnanomiamidolphins,<a href="httpwwwnxedpuuecnpage65html">radiomiamidolphins</a> radiomiamidolphins,<a href="httpwwwnxedpuuecnpage51html">bestofemailcom</a> bestofemailcom,<a href="httpwwwnxedpuuecnpage70html">newaniverseryipodrumours</a> newaniverseryipodrumours,<a href="httpwwwnxedpuuecnpage76html">friends11groupjoinedprofileupdated</a> friends11groupjoinedprofileupdated,

  8. <a href="httpwwwmenhzyjdcnpage36html">adidaslogotype</a> adidaslogotype,<a href="httpwwwmenhzyjdcnpage35html">juegosbiblicosparajovenes</a> juegosbiblicosparajovenes,<a href="httpwwwmenhzyjdcnpage28html">sanitaryreducer6x4</a> sanitaryreducer6x4,<a href="httpwwwmenhzyjdcnpage38html">foriraqthesalvadoroptionbecomesrealitybymaxfuller</a> foriraqthesalvadoroptionbecomesrealitybymaxfuller,<a href="httpwwwmenhzyjdcnpage29html">nemomuseumamsterdam</a> nemomuseumamsterdam,<a href="httpwwwmenhzyjdcnpage37html">starkstatecantonohio</a> starkstatecantonohio,<a href="httpwwwmenhzyjdcnpage34html">howmanysnowboardingmanufacturersarethereintheworld</a> howmanysnowboardingmanufacturersarethereintheworld,<a href="httpwwwmenhzyjdcnpage34html">snowboardinggirlmooning</a> snowboardinggirlmooning,<a href="httpwwwmenhzyjdcnpage22html"></a> ,<a href="httpwwwmenhzyjdcnpage44html">usingtotalrecorderwithminidisc</a> usingtotalrecorderwithminidisc,

  9. Robert Jacky says:

    Toothache has a number of causes. Find out what they are, and learn about two toothache remedies that may help.

  10. Robert Jacky says:

    Find many tips and techniques to prevent tooth loss.Those tips will surely

    help you to get away from toothache.

Skip to main content