How to: Activate Add-ins with Different Isolation and Security Levels

You can activate an add-in by using different application domain and process configurations to suit your isolation and security requirements. The Activate method overloads of the AddInToken class provide the following choices for activating an add-in:

  • In its own application domain, which is automatically generated by the system.

  • In an existing application domain, with or without other add-ins.

  • In the same environment (application domain and process) as another add-in.

  • In a new, external process that is separate from the host's process. The new process can include other add-ins that share that process.

When you activate an add-in in a new application domain or process, you must specify a security level by using either an AddInSecurityLevel or a PermissionSet object. For more information about activating add-ins, see Add-in Activation.

The following procedures show how to implement these activation options by using the Activate method overloads. The examples assume that the pipeline segment and add-in caches have been built, and that one or more add-ins have been found and returned in an AddInToken collection named tokens. For more information about how to build the cache files and find add-ins, see Add-in Discovery.

To activate an add-in in a new application domain

  • Use the Activate<T>(AddInSecurityLevel) or Activate<T>(PermissionSet) method overload.

    'Ask the user which add-in they would like to use. 
    Dim selectedToken As AddInToken = ChooseAddIn(tokens)
    'Activate the selected AddInToken in a new 
    'application domain with the Internet trust level. 
    Dim CalcAddIn As Calculator = selectedToken.Activate(Of Calculator)(AddInSecurityLevel.Internet)
    'Run the add-in using a custom method.
    RunCalculator(CalcAddIn)
    
    //Ask the user which add-in they would like to use.
    AddInToken selectedToken = ChooseAddIn(tokens);
    
    //Activate the selected AddInToken in a new 
    //application domain with the Internet trust level.
    Calculator CalcAddIn = selectedToken.Activate<Calculator>(AddInSecurityLevel.Internet);
    
    //Run the add-in using a custom method.
    RunCalculator(CalcAddIn);
    

To activate an add-in in the same application domain as another add-in

  • Use the Activate<T>(AppDomain) method overload.

    ' Get the application domain 
    ' of an existing add-in (CalcAddIn). 
    
    Dim aiCtrl As AddInController = AddInController.GetAddInController(CalcAddIn)
    Dim AddInAppDom As AppDomain = aiCtrl.AppDomain
    
    ' Activate another add-in in the same appliation domain. 
    Dim CalcAddIn3 As Calculator = selectedToken2.Activate(Of Calculator)(AddInAppDom)
    
    ' Show that the CalcAddIn3 was loaded 
    ' into CalcCaddIn's application domain. 
    Dim aic As AddInController = AddInController.GetAddInController(CalcAddIn3)
    Console.WriteLine("Add-in loaded into existing application domain: {0}", _
     aic.AppDomain.Equals(AddInAppDom))
    
    // Get the application domain 
    // of an existing add-in (CalcAddIn).
    AddInController aiCtrl = AddInController.GetAddInController(CalcAddIn);
    AppDomain AddInAppDom = aiCtrl.AppDomain;
    
    // Activate another add-in in the same application domain.
    Calculator CalcAddIn3 =
        selectedToken2.Activate<Calculator>(AddInAppDom);
    
    // Show that CalcAddIn3 was loaded 
    // into CalcAddIn's application domain.
    AddInController aic = AddInController.GetAddInController(CalcAddIn3);
    Console.WriteLine("Add-in loaded into existing application domain: {0}",
        aic.AppDomain.Equals(AddInAppDom));
    

To activate an add-in in the same application domain and process as another add-in

  • Use the Activate<T>(AddInEnvironment) method overload.

    ' Get the AddInController of a  
    ' currently activated add-in (CalcAddIn). 
    Dim aiController As AddInController = AddInController.GetAddInController(CalcAddIn)
    
    ' Select another token. 
    Dim selectedToken2 As AddInToken = ChooseAddIn(tokens)
    
    ' Activate a second add-in, CalcAddIn2, in the same 
    ' appliation domain and process as the first add-in by passing 
    ' the first add-in's AddInEnvironment object to the Activate method. 
    
    Dim aiEnvironment As AddInEnvironment = aiController.AddInEnvironment
    Dim CalcAddIn2 As Calculator = _
        selectedToken2.Activate(Of Calculator)(aiEnvironment)
    
    ' Get the AddInController for the second add-in to compare environments. 
    Dim aiController2 As AddInController = AddInController.GetAddInController(CalcAddIn2)
    
    Console.WriteLine("Add-ins in same application domain: {0}", _
        aiController.AppDomain.Equals(aiController2.AppDomain))
    Console.WriteLine("Add-ins in same process: {0}", _
        aiEnvironment.Process.Equals(aiController2.AddInEnvironment.Process))
    
    // Get the AddInController of a  
    // currently actived add-in (CalcAddIn).
    AddInController aiController = AddInController.GetAddInController(CalcAddIn);
    
    // Select another token.
    AddInToken selectedToken2 = ChooseAddIn(tokens);
    
    // Activate a second add-in, CalcAddIn2, in the same 
    // appliation domain and process as the first add-in by passing 
    // the first add-in's AddInEnvironment object to the Activate method.
    AddInEnvironment aiEnvironment = aiController.AddInEnvironment;
    Calculator CalcAddIn2 =
        selectedToken2.Activate<Calculator>(aiEnvironment);
    
    // Get the AddInController for the second add-in to compare environments.
    AddInController aiController2 = AddInController.GetAddInController(CalcAddIn2);
    Console.WriteLine("Add-ins in same application domain: {0}", aiController.AppDomain.Equals(aiController2.AppDomain));
    Console.WriteLine("Add-ins in same process: {0}", aiEnvironment.Process.Equals(aiController2.AddInEnvironment.Process));
    

To activate an add-in in a new process

  • Use the Activate<T>(AddInProcess, AddInSecurityLevel) or Activate<T>(AddInProcess, PermissionSet) method overload.

    ' Create an external process. 
    Dim pExternal As New AddInProcess()
    
    ' Activate an add-in in the external process 
    ' with a full trust security level. 
    Dim CalcAddIn4 As Calculator = _
        selectedToken.Activate(Of Calculator)(pExternal, _
            AddInSecurityLevel.FullTrust)
    
    ' Show that the add-in is an an external process 
    ' by verifying that it is not in the current (host's) process. 
    Dim AddinCtl As AddInController = AddInController.GetAddInController(CalcAddIn4)
    Console.WriteLine("Add-in in host's process: {0}", _
     AddinCtl.AddInEnvironment.Process.IsCurrentProcess)
    
    // Create an external process.
    AddInProcess pExternal = new AddInProcess();
    
    // Activate an add-in in the external process 
    // with a full trust security level.
    Calculator CalcAddIn4 =
        selectedToken.Activate<Calculator>(pExternal,
        AddInSecurityLevel.FullTrust);
    
    // Show that the add-in is an an external process 
    // by verifying that it is not in the current (host's) process.
    AddInController AddinCtl = AddInController.GetAddInController(CalcAddIn4);
    Console.WriteLine("Add-in in host's process: {0}",
        AddinCtl.AddInEnvironment.Process.IsCurrentProcess);
    

See Also

Concepts

Add-in Discovery

Add-in Developer Experience

Other Resources

Add-ins and Extensibility