How to: Troubleshoot Services

There are several common problems with obtaining services:

  • The service is not registered with Visual Studio.

  • The service is requested by interface type and not by service type.

  • The VSPackage requesting the service has not been sited.

  • The wrong service provider is used.

If the requested service cannot be obtained, the call to GetService returns null. You should always test for null after requesting a service:

Dim log As IVsActivityLog = TryCast(GetService(GetType(SVsActivityLog)), IVsActivityLog)
If log Is Nothing Then 
    Return 
End If
IVsActivityLog log = GetService(typeof(SVsActivityLog)) as IVsActivityLog;
if (log == null) return;

To troubleshoot a service

  1. Examine the system registry to see whether the service has been correctly registered. For more information, see Registering Services.

  2. Use the service type and not the interface type when you call GetService. When requesting a service from Visual Studio, Package extracts the GUID from the type. A service will not be found if the following conditions exist:

    1. An interface type is passed to GetService instead of the service type.

    2. No GUID is explicitly assigned to the interface. Therefore, the system creates a default GUID for an object as needed.

  3. Be sure the VSPackage requesting the service has been sited. Visual Studio sites a VSPackage after constructing it and before calling Initialize.

    If you have code in a VSPackage constructor that needs a service, move it to the Initialize method.

  4. Be sure that you are using the correct service provider.

    Not all service providers are alike. The service provider that Visual Studio passes to a tool window differs from the one it passes to a VSPackage. The tool window service provider knows about STrackSelection, but does not know about SVsRunningDocumentTable. You can call GetGlobalService to get a VSPackage service provider from within a tool window. For more information, see How to: Use GetGlobalService.

    If a tool window hosts a user control or any other control container, the container will be sited by the Windows component model and will not have access to any Visual Studio services. You can call GetGlobalService to get a VSPackage service provider from within a control container. For more information, see How to: Use GetGlobalService.

See Also

Concepts

List of Available Services

Service Essentials

Other Resources

Services