共用方式為


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.

注意

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
    
    [ProvideService(typeof(SMyGlobalService))]
    public sealed class ServicesPackage : Package
    

    ProvideServiceAttribute registers SMyGlobalService with Visual Studio. Only the global service must be registered. 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
    
    public ServicesPackage()
    {
        IServiceContainer serviceContainer = this as IServiceContainer;
        ServiceCreatorCallback callback =
           new ServiceCreatorCallback(CreateService);
        serviceContainer.AddService(typeof(SMyGlobalService), callback, true);
        serviceContainer.AddService(typeof(SMyLocalService), callback);
    }
    

    The true flag instructs the service container to make SMyGlobalService a global service.

    注意

    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
    
    private object CreateService(IServiceContainer container, Type serviceType)
    {
        if (typeof(SMyGlobalService) == serviceType)
            return new MyGlobalService(this);
    
        if (typeof(SMyLocalService) == serviceType)
            return new MyLocalService(this);
    
        return null;
    }
    

    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
    
    public class MyGlobalService : SMyGlobalService, IMyGlobalService
    {
        private IServiceProvider serviceProvider;
        public MyGlobalService(IServiceProvider sp)
        {
            Trace.WriteLine(
               "Constructing a new instance of MyGlobalService");
            serviceProvider = sp;
        }
        // Implement the methods of IMyGlobalService here.
    }
    

    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
    
    public class MyLocalService : SMyLocalService, IMyLocalService
    {
        private IServiceProvider serviceProvider;
        public MyLocalService(IServiceProvider sp)
        {
            Trace.WriteLine(
               "Constructing a new instance of MyLocalService");
            serviceProvider = sp;
        }
        // Implement the methods of IMyLocalService here.
    }
    

See Also

Tasks

How to: Consume a Service

Concepts

Visual Studio Extensibility Samples

Service Essentials

Other Resources

Services