VSTO 外接程序的体系结构Architecture of VSTO Add-ins

使用 Visual Studio 中的 Office 开发人员工具创建的 VSTO 外接程序具有强调稳定性和安全性的体系结构功能,并使其能够与 Microsoft Office 紧密合作。VSTO Add-ins created by using the Office developer tools in Visual Studio have architectural features that emphasize stability and security, and enable them to work closely with Microsoft Office. 本主题介绍 VSTO 外接程序的以下方面:This topic describes the following aspects of VSTO Add-ins:

了解 VSTO 外接程序Understand VSTO Add-ins

当您使用在 Visual Studio 中的 Office 开发人员工具生成 VSTO 外接程序中时,您将创建由 Microsoft Office 应用程序加载的托管的代码程序集。When you use the Office developer tools in Visual Studio to build a VSTO Add-in, you create a managed code assembly that is loaded by a Microsoft Office application. 加载该程序集后,VSTO 外接程序可以响应在应用程序中引发的事件(例如,用户单击菜单项时)。After the assembly is loaded, the VSTO Add-in can respond to events that are raised in the application (for example, when a user clicks a menu item). VSTO 外接程序也可以调入对象模型,以便实现应用程序自动化和扩展应用程序,并且它可以使用 .NET Framework.NET Framework中的任何类。The VSTO Add-in can also call into the object model to automate and extend the application, and it can use any of the classes in the .NET Framework.NET Framework.

程序集通过应用程序的主互操作程序集与应用程序的 COM 组件进行通信。The assembly communicates with the application's COM components through the primary interop assembly of the application. 有关详细信息,请参阅Office 主互操作程序集Office 解决方案开发概述(VSTO)For more information, see Office primary interop assemblies and Office solutions development overview (VSTO).

如果为应用程序安装了多个 VSTO 外接程序,那么,每个 VSTO 外接程序都会加载到不同的应用程序域中。If multiple VSTO Add-ins are installed for an application, each VSTO Add-in is loaded in a different application domain. 这意味着某个行为不正确的 VSTO 外接程序不会导致其他 VSTO 外接程序失败。This means that one VSTO Add-in that behaves incorrectly cannot cause other VSTO Add-ins to fail. 这还有助于确保在关闭应用程序时,所有 VSTO 外接程序程序集都将从内存中卸载。It also helps to ensure that when the application is closed, all the VSTO Add-in assemblies are unloaded from memory. 有关应用程序域的详细信息,请参阅应用程序域For more information about application domains, see Application domains.

Note

对于使用 Visual Studio 中的 Office 开发人员工具创建的 VSTO 外接程序,仅当最终用户启动主机 Microsoft Office 应用程序时才会使用。VSTO Add-ins that you create by using the Office developer tools in Visual Studio are designed to be used only when the host Microsoft Office application is started by an end user. 如果以编程方式(例如,通过使用自动化)启动应用程序,VSTO 外接程序可能不会按预期工作。If the application is started programmatically (for example, by using Automation), the VSTO Add-in might not work as expected.

VSTO 外接程序的组件Components of VSTO Add-ins

尽管 VSTO 外接程序程序集是主要组件,但有其他若干组件对 Microsoft Office 应用程序如何发现和加载 VSTO 外接程序起重要作用。Although the VSTO Add-in assembly is the main component, there are several other components that play an important role in how Microsoft Office applications discover and load VSTO Add-ins.

注册表项Registry entries

Microsoft Office 应用程序通过查找一组注册表项来发现 VSTO 外接程序。Microsoft Office applications discover VSTO Add-ins by looking for a set of registry entries. 有关使用 VSTO 外接程序的注册表项的完整列表,请参阅VSTO 外接程序的注册表项For a complete list of the registry entries used by VSTO Add-ins, see Registry entries for VSTO Add-ins.

生成解决方案时,Visual Studio 会在开发计算机上创建所有必需的注册表项,以便你调试和运行 VSTO 外接程序。When you build your solution, Visual Studio creates all of the required registry entries on the development computer so that you can debug and run your VSTO Add-in. 有关详细信息,请参阅生成 Office 解决方案For more information, see Build Office solutions.

如果使用 ClickOnce 来部署你的解决方案,安装程序自动生成的发布过程的最终用户计算机上创建注册表项。If you use ClickOnce to deploy your solution, the Setup program generated by the publish process automatically creates the registry keys on the end-user computer. 有关详细信息,请参阅通过使用 ClickOnce 部署 Office 解决方案For more information, see Deploy an Office solution by using ClickOnce.

部署清单和应用程序清单Deployment manifest and application manifest

VSTO 外接程序使用部署清单和应用程序清单来标识和加载 VSTO 外接程序程序集的最新版本。VSTO Add-ins use deployment manifests and application manifests to identify and load the most current version of the VSTO Add-in assembly. 部署清单指向当前应用程序清单。The deployment manifest points to the current application manifest. 应用程序清单指向 VSTO 外接程序程序集,并指定要在程序集中执行的入口点类。The application manifest points to the VSTO Add-in assembly, and specifies the entry point class to execute in the assembly. 有关详细信息,请参阅应用程序和部署清单在 Office 解决方案中For more information, see Application and deployment manifests in Office solutions.

Visual Studio Tools for Office RuntimeVisual Studio Tools for Office Runtime

若要运行 VSTO 加载项通过使用 Visual Studio 中的 Office 开发人员工具创建的最终用户计算机必须具有Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime安装。To run VSTO Add-ins that are created by using the Office developer tools in Visual Studio, end-user computers must have the Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime installed. 运行时包括非托管组件和一组托管程序集。The runtime includes unmanaged components and a set of managed assemblies. 非托管组件加载 VSTO 外接程序程序集。The unmanaged components load the VSTO Add-in assembly. 托管程序集提供 VSTO 外接程序代码用于实现主机应用程序自动化和扩展主机应用程序的对象模型。The managed assemblies provide the object model that your VSTO Add-in code uses to automate and extend the host application.

有关详细信息,请参阅Visual Studio Tools for Office runtime 概述For more information, see Visual Studio Tools for Office runtime overview.

VSTO 外接程序与 Microsoft Office 应用程序的工作原理How VSTO Add-ins work with Microsoft Office applications

当用户启动 Microsoft Office 应用程序时,该应用程序使用部署清单和应用程序清单来查找并加载 VSTO 外接程序程序集的最新版本。When a user starts a Microsoft Office application, the application uses the deployment manifest and the application manifest to locate and load the most current version of the VSTO Add-in assembly. 下图显示了这些 VSTO 外接程序的基本体系结构。The following illustration shows the basic architecture of these VSTO Add-ins.

2007 office 外接程序体系结构2007 Office add-in architecture

Note

在面向 .NET Framework 4.NET Framework 4.NET Framework 4.5.NET Framework 4.5的 Office 解决方案中,解决方案通过使用嵌入到解决方案程序集中的 PIA 类型信息调入主机应用程序的对象模型,而不是直接调入 PIA。In Office solutions that target the .NET Framework 4.NET Framework 4 or the .NET Framework 4.5.NET Framework 4.5, solutions call into the object model of the host application by using PIA type information that is embedded in the solution assembly, instead of calling into the PIA directly. 有关详细信息,请参阅设计和创建 Office 解决方案For more information, see Design and create Office solutions.

加载过程Loading process

用户启动应用程序时,会执行以下步骤:The following steps occur when a user starts an application:

  1. 应用程序检查注册表中是否存在标识使用 Visual Studio 中的 Office 开发人员工具创建的 VSTO 外接程序的项。The application checks the registry for entries that identify VSTO Add-ins that were created by using the Office developer tools in Visual Studio.

  2. 如果应用程序找到这些注册表项,则该应用程序加载 VSTOEE.dll,后者会加载 VSTOLoader.dll。If the application finds these registry entries, the application loads VSTOEE.dll, which loads VSTOLoader.dll. 这些非托管 DLL 是 Visual Studio 2010 Tools for Office Runtime 的加载程序组件。These are unmanaged DLLs that are the loader components for the Visual Studio 2010 Tools for Office Runtime. 有关详细信息,请参阅Visual Studio Tools for Office runtime 概述For more information, see Visual Studio Tools for Office runtime overview.

  3. VSTOLoader.dll加载.NET Framework.NET Framework,并启动的托管的部分Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtimeVSTOLoader.dll loads the .NET Framework.NET Framework and starts the managed portion of the Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime.

  4. Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime 检查清单更新,并下载最新的应用程序和部署清单。The Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime checks for manifest updates, and downloads the most recent application and deployment manifests.

  5. Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime 执行一系列安全检查。The Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime performs a series of security checks. 有关详细信息,请参阅保护 Office 解决方案For more information, see Secure Office solutions.

  6. 如果 VSTO 外接程序受信任,可以运行,则 Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime 使用部署清单和应用程序清单来检查程序集更新。If the VSTO Add-in is trusted to run, the Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime uses the deployment manifest and application manifest to check for assembly updates. 如果集有新版本的程序集可用,则运行时会将新版本的程序集下载到客户端计算机上的 ClickOnceClickOnce 缓存中。If a new version of the assembly is available, the runtime downloads the new version of the assembly to the ClickOnceClickOnce cache on the client computer. 有关详细信息,请参阅部署 Office 解决方案For more information, see Deploy an Office solution.

  7. Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime 创建一个要在其中加载 VSTO 外接程序程序集的新应用程序域。The Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime creates a new application domain in which to load the VSTO Add-in assembly.

  8. Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime 将 VSTO 外接程序程序集加载到应用程序域中。The Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime loads the VSTO Add-in assembly into the application domain.

  9. 如果你已重写 Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime 方法,则 RequestComAddInAutomationService 将在 VSTO 外接程序中调用该方法。The Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime calls the RequestComAddInAutomationService method in your VSTO Add-in, if you have overridden it.

    可以选择重写此方法,以向其他 Microsoft Office 解决方案公开 VSTO 外接程序中的对象。You can optionally override this method to expose an object in your VSTO Add-in to other Microsoft Office solutions. 有关详细信息,请参阅从其他 Office 解决方案调用 VSTO 外接程序中的代码For more information, see Call code in VSTO Add-ins from other Office solutions.

  10. 如果你已重写 Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime 方法,则 RequestService 将在 VSTO 外接程序中调用该方法。The Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime calls the RequestService method in your VSTO Add-in, if you have overridden it.

    可以选择替代此方法,以便通过返回实现扩展性接口的对象来扩展 Microsoft Office 功能。You can optionally override this method to extend a Microsoft Office feature by returning an object that implements an extensibility interface. 有关详细信息,请参阅使用扩展性接口自定义 UI 功能For more information, see Customize UI features by using extensibility interfaces.

    Note

    Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime 为主机应用程序支持的每个扩展性接口分别调用 RequestService 方法。The Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime makes separate calls to the RequestService method for each extensibility interface that is supported by the host application. 尽管对 RequestService 方法的第一次调用通常发生在调用 ThisAddIn_Startup 方法之前,但 VSTO 外接程序不应作出有关 RequestService 方法的调用时间和次数的任何假设。Although the first call to the RequestService method usually happens before the call to the ThisAddIn_Startup method, your VSTO Add-in should not make any assumptions about when the RequestService method will be called, or how many times it will be called.

  11. Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime 在 VSTO 外接程序中调用 ThisAddIn_Startup 方法。The Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime calls the ThisAddIn_Startup method in your VSTO Add-in. 此方法是 Startup 事件的默认事件处理程序。This method is the default event handler for the Startup event. 有关详细信息,请参阅Office 项目中的事件For more information, see Events in Office projects.

请参阅See also