Managing the lifecycle of an AddIn is not trivial. AddIn may be in-process or out-of process, AddIns may share one AppDomain, AddIn my stay in its own AppDomain, AdddIn may stay in Host’s AppDomain, one AddIn may have multiple host views, AddIn may have disposable resources. To handle all these scenarios, we need to do some bookkeeping when we activate an AddIn.
We store information of an AddIn in AddInController during activation and keep all AddInController information in a static LinkList for later reference. Since our hosting API will do the most work, host developer can simply shutdown an AddIn with two lines of code like below.
AddInController controller = AddInController.GetAddInController(hav);
Here are the few steps happened in background.
- Dispose method will be called for HostAdapter
- Dispose method will be called for AddInAdapter
- Dispose method will be called for ActivationWorker (helper class to create AddIn AppDomain)
- Unload AddIn AppDomain if the AddIn is the owner (Multiple AddIns in one AppDomain, only the first one is the owner)
- ShutDown AddInProcess if there is no AddIn AppDomain in the process.
- Update the AddInController LinkList
Using the hosting API is only one way to do the clean up. There is another way to do the cleanup – GC. When we set host adapter view to null and wait for GC to happen, clean up can be done as well. GC is not deterministic in releasing resources which is why we provide AddInController.Shutdown for the Host to use. If we handle IContract correctly with LifetimeTokenHandle, AddIn AppDomain will go away during GC time by its own with the magic code in ContractBase class.
Next topic will be LifetimeToken and LifetimeTokenHandle.