Udostępnij za pośrednictwem


Discovering and Loading Add-Ins

The.NET Framework provides an API that you can use to discover and load add-ins for a host application. This API enables you to define your own discovery model for add-ins, and to control the environment in which add-ins are loaded, including the process, application domain, and security context.

Before you write code to discover and load add-ins, there are several decisions you should make first. For example, you should decide which add-in deployment model you want the host application to use, and how you want to integrate the code that discovers and loads add-ins into the host application. For more information, see Considerations for Loading Add-Ins.

Overview of Discovering and Loading Add-Ins

The process of discovering and loading add-ins consists of the following main steps:

  1. Define a host item provider and type map provider in the host application. These are services that enable the host application and add-ins to call into each other and use each other's types. The implementations of these services will vary from application to application. For more information, see Implementing Host Services.

  2. Discover and load add-ins by using the methods of the AddInStore and AddInToken classes in the .NET Framework, or by using one of the extension methods that Visual Studio Tools for Applications adds to these classes. For more information, see Discovering Add-Ins and Loading Add-Ins.

  3. Initialize the add-ins by calling methods of the add-in view that was instantiated when you loaded the add-in. For more information, see Initializing Add-Ins.

For a walkthrough that demonstrates this process, see Walkthrough: Modifying an Application to Load Add-Ins.

Discovering Add-Ins

The host application discovers add-ins by searching in one or more locations on the local computer for assemblies that implement one of the add-in views defined by Visual Studio Tools for Applications. The add-in view is the IEntryPoint interface, or one of the interfaces that derive from it. For more information about add-in views, see Understanding the Add-in Pipeline in Visual Studio Tools for Applications.

To discover add-ins, use the FindAddIn method or one of the FindAddIns methods of the AddInStore class in the .NET Framework. For each assembly that implements an add-in view, these methods return an AddInToken object that represents an add-in. The host application can then use the AddInToken to load the add-in.

The add-in discovery methods have parameters that specify the following information:

  • The type of the add-in view to discover. This is the IEntryPoint interface, or an interface that derives from it.

  • The path of the add-in pipeline that will be used to exchange data between the host application and the add-in. This path is always the Visual Studio Tools for Applications pipeline directory (%CommonProgramFiles%\Microsoft Shared\VSTA\Pipeline). For more information about the add-in pipeline, see Understanding the Add-in Pipeline in Visual Studio Tools for Applications.

  • One or more paths in which to look for add-ins.

For more information about how to discover add-ins, see Add-in Discovery in the MSDN Library for Visual Studio 2008.

Using Discovery Methods Provided by Visual Studio Tools for Applications

Visual Studio Tools for Applications provides several static FindAddIns methods in addition to those provided by the .NET Framework. These methods are defined in the AddInStoreExtensions class. You can optionally use these methods instead of the methods in the AddInStore class to help simplify the process of discovering add-ins.

The AddInStoreExtensions class includes the following methods:

Loading Add-Ins

To load an add-in, call one of the Activate methods of the AddInToken object that the host application obtained when it discovered the add-in. The Activate methods load the add-in assembly, instantiate the class that implements the add-in view, and return the add-in view object to the host application.

You have several options for loading the add-in assembly:

  • You can load each add-in into its own application domain, or you can load multiple add-ins in the same application domain.

  • You can load add-ins into an external process that is separate from the host application's process.

  • You can specify the permission set or trust level of the application domain or process in which the add-in is loaded.

For more information, see Add-in Activation and How to: Activate Add-ins with Different Isolation and Security Levels in the MSDN Library for Visual Studio 2008.

Visual Studio Tools for Applications also provides several Activate extension methods in the AddInTokenExtensions class. These methods are intended to be used only if you implement a ClickOnce deployment model for your application. For more information, see Deploying Add-ins by Using ClickOnce

Initializing Add-Ins

After the host application loads an add-in, it can initialize the add-in by calling methods of the IEntryPoint object that was returned by the Activate method.

The following table lists the initialization methods of the IEntryPoint interface. These methods are intended to be called in the order specified in this table. Other add-in views that derive from IEntryPoint provide additional initialization methods that you can use in advanced scenarios.

Method

Description

Initialize

Performs tasks that must be completed before any other code in the add-in can run. By default, this method executes code that ProxyGen.exe automatically generates in the entry point class in the proxy assembly. For more information, see Architecture of Generated Proxy Code.

The serviceProvider parameter is an IServiceProvider that must contain the host item provider and type map provider services for the host application. For more information, see Implementing Host Services.

InitializeDataBindings

Runs code that binds any data-bound objects in the add-in to their data sources.

FinishInitialization

Runs code that completes the initialization of the add-in. This method also begins executing the add-in developer's code.

See Also

Tasks

Walkthrough: Modifying an Application to Load Add-Ins

Concepts

Considerations for Loading Add-Ins

Implementing Host Services

Understanding the Add-in Pipeline in Visual Studio Tools for Applications

Preventing Unexpected Application Failures from Partially Trusted Add-Ins