Implementing Host Services
After your host application loads an add-in, Visual Studio Tools for Applications uses two services that you must implement in the host application. These services are classes that implement interfaces defined by Visual Studio Tools for Applications. After you define the services in the host application, you must expose them to Visual Studio Tools for Applications.
Every host application that integrates Visual Studio Tools for Applications must provide the following services:
A host item provider. This is an object that implements the IHostItemProvider interface. This interface defines a method that returns an object that add-ins use to call into the host application. For more information, see Exposing Host Objects to Add-Ins.
A type map provider. This is an object that implements the ITypeMapProvider interface. This interface defines methods that return the canonical name that is associated with a specified host type, or the host type that is associated with a specified canonical name. For more information, see Mapping Host Types to Proxy Types.
You expose the services in your host application to Visual Studio Tools for Applications after your application loads an add-in. When the application loads the add-in, it receives an IEntryPoint object that represents the add-in. To expose the services, your application must pass an IServiceProvider that contains the services to the Initialize method of this object. For more information about IEntryPoint and the process of loading add-ins, see Considerations for Loading Add-Ins.
The most common way to expose the services is to instantiate each service and add the objects to a ServiceContainer (an object that implements IServiceProvider, and also acts as a container for services). You can add all of the instantiated services to the ServiceContainer object and pass it to the Initialize method.
The following code example demonstrates this process.
// Instantiate the host item provider and type map provider services. IHostItemProvider itemProvider = new HostItemProvider(this.application); ITypeMapProvider typeMapProvider = new HostTypeMapProvider(); // Add the services to a ServiceContainer. ServiceContainer container = new ServiceContainer(); container.AddService(typeof(IHostItemProvider), itemProvider); container.AddService(typeof(ITypeMapProvider), typeMapProvider); // Activate the add-in and pass the services to the Initialize method. Collection<AddInToken> addInToken = AddInStore.FindAddIn(typeof(IEntryPoint), AddInStoreExtensions.DefaultPipelinePath, addInPath, startUpClass); IEntryPoint addIn = addInToken.Activate<IEntryPoint>(AddInSecurityLevel.FullTrust); addIn.Initialize(container);
This is a modified version of code from the VstaRunTimeIntegration.cs file in the ShapeAppBasicCSharp sample. To clarify the process, some error checking code has been removed, and code has been merged from several different methods. For more information about this sample, see ShapeApp Samples (Visual Studio Tools for Applications).