How to: Provide a Service (C#) :

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.
Implement IServiceProvider.
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#). It provides the local service, SMyLocalService, and the global service, SMyGlobalService.
To provide a service
Add the ProvideServiceAttribute to the VSPackage providing the global service.
[ProvideService(typeof(SMyGlobalService))]
public sealed class ServicesPackage : Package
{
The ProvideServiceAttribute registers SMyGlobalService with Visual Studio. Only the global service must be registered.
Add callback methods to the service container to create the services.
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.
Implement the callback method.
private object CreateService(IServiceContainer container, Type serviceType)
{
if (typeof(SMyGlobalService) == serviceType)
return new MyGlobalService(this);

if (typeof(SMyLocalService) == serviceType)
return new MyLocalService(this);
}

CreateService creates either SMyGlobalService or SMyLocalService on demand.

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

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.