如何:使用不同的隔离和安全性激活外接程序
可以使用不同的应用程序域和进程配置激活外接应用程序,以满足您的隔离和安全要求。 AddInToken 类的 Activate 方法重载为激活外接应用程序提供以下选择:
在其自己的由系统自动生成的应用程序域中。
在包含或不包含其他外接程序的现有应用程序域中。
在另一外接程序所处的环境(应用程序域和进程)中。
在一个新的、与宿主的进程不同的外部进程中。 该新进程可以包含共享该进程的其他外接程序。
当激活新应用程序域或进程中的外接程序时,必须使用 AddInSecurityLevel 或 PermissionSet 对象指定一个安全级别。 有关激活外接程序的更多信息,请参见外接程序的激活。
下面的过程演示如何使用 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);