How to: Provide a Service
A VSPackage can provide services that other VSPackages can consume. To provide a service, a VSPackage must perform the following tasks:
-
Register the service with Visual Studio.
For more information, see Registering Services.
-
Implement IServiceProvider.
The Package class implements both IServiceProvider and IServiceContainer. IServiceContainer holds callback methods to provide local and global services on demand.
Note
|
|---|
|
When a VSPackage is about to be unloaded, Visual Studio waits until all requests for services that a VSPackage provides have been delivered. It does not allow new requests for these services. Therefore, VSPackages should not explicitly call the RevokeService method to revoke a service when unloading. |
The following code is taken from the Reference.Services sample (C#) in the Visual Studio Extensibility Samples. It provides the local service, SMyLocalService, and the global service, SMyGlobalService.
To provide a service
-
Add the ProvideServiceAttribute to the VSPackage that provides the global service.
ProvideServiceAttribute registers SMyGlobalService with Visual Studio. Only the global service must be registered.
Use the ProvideServiceOverrideAttribute to register a global service that replaces any other service with the same name. Note that only one override of a service is allowed.
For more information, see How to: Register a Service.
-
Add callback methods to the service container to create the services.
Public Sub New() Dim serviceContainer As IServiceContainer = TryCast(Me, IServiceContainer) Dim callback As New ServiceCreatorCallback(AddressOf CreateService) serviceContainer.AddService(GetType(SMyGlobalService), callback, True) serviceContainer.AddService(GetType(SMyLocalService), callback) End Sub
The true flag instructs the service container to make SMyGlobalService a global service.
Note
Visual Studio can reject a request to provide a service. It does so if another VSPackage already provides the service.
-
Implement the callback method.
CreateService creates either SMyGlobalService or SMyLocalService on demand.
-
Implement the global service class.
Public Class MyGlobalService Inherits SMyGlobalService Implements IMyGlobalService Private serviceProvider As IServiceProvider Public Sub New(ByVal sp As IServiceProvider) Trace.WriteLine("Constructing a new instance of MyGlobalService") serviceProvider = sp End Sub ' Implement the methods of IMyGlobalService here. End Class
The MyGlobalService class implements both SMyGlobalService and the IMyGlobalInterface interface provided by this service. The service provider passed to the constructor is cached so that the interface methods have access to other services.
-
Implement the local service class.
Public Class MyLocalService Inherits SMyLocalService Implements IMyLocalService Private serviceProvider As IServiceProvider Public Sub New(ByVal sp As IServiceProvider) Trace.WriteLine("Constructing a new instance of MyLocalService") serviceProvider = sp End Sub ' Implement the methods of IMyLocalService here. End Class