This documentation is archived and is not being maintained.

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:

The Package class implements both IServiceProvider and IServiceContainer. IServiceContainer holds callback methods to provide local and global services on demand.

Note 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

  1. Add the ProvideServiceAttribute to the VSPackage that provides the global service.

    <ProvideService(GetType(SMyGlobalService))> _
    Public NotInheritable Class ServicesPackage
        Inherits Package
    

    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.

  2. 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 Note

    Visual Studio can reject a request to provide a service. It does so if another VSPackage already provides the service.

  3. Implement the callback method.

    Private Function CreateService(ByVal container As IServiceContainer, ByVal serviceType As Type) As Object 
        If GetType(SMyGlobalService) Is serviceType Then 
            Return New MyGlobalService(Me)
        End If 
    
        If GetType(SMyLocalService) Is serviceType Then 
            Return New MyLocalService(Me)
        End If 
    End Function
    

    CreateService creates either SMyGlobalService or SMyLocalService on demand.

  4. 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.

  5. 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
    
Show: