共用方式為


HOW TO:以不同的隔離和安全性層級啟動增益集

您可以使用不同的應用程式定義域和處理序 (Process) 組態,啟用符合您隔離和安全性需求的增益集。 AddInToken 類別的 Activate 方法多載提供了下列啟動增益集的選項:

  • 在其自己的應用程式定義域中,這個定義域是由系統自動產生。

  • 在現有的應用程式定義域中,無論有沒有其他增益集。

  • 在與其他增益集相同的環境 (應用程式定義域和處理序) 中。

  • 在與主應用程式之處理序不同的新外部處理序中。 新的處理序可以包含其他共用該處理序的增益集。

當您在新的應用程式定義域或處理序中啟動增益集時,必須使用 AddInSecurityLevelPermissionSet 物件指定安全性層級。 如需啟動增益集的詳細資訊,請參閱增益集啟動

下列程序將說明如何使用 Activate 方法或多載來實作這些啟動選項。 這些範例假設已建置管線區段和增益集快取,而且已在名為 tokens 的 AddInToken 集合中找到一個或多個增益集並傳回。 如需如何建置快取檔案和尋找增益集的詳細資訊,請參閱增益集探索

若要在新的應用程式定義域中啟動增益集

  • 使用 Activate<T>(AddInSecurityLevel)Activate<T>(PermissionSet) 方法多載。

    '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);
    

若要在與其他增益集相同的應用程式定義域中啟動增益集

  • 使用 Activate<T>(AppDomain) 方法多載。

    ' 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));
    

若要在與其他增益集相同的應用程式定義域和處理序中啟動增益集

  • 使用 Activate<T>(AddInEnvironment) 方法多載。

    ' 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));
    

若要在新的處理序中啟動增益集

  • 使用 Activate<T>(AddInProcess, AddInSecurityLevel)Activate<T>(AddInProcess, PermissionSet) 方法多載。

    ' 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);
    

請參閱

概念

增益集探索

增益集開發人員經驗

增益集和擴充性