Can't get a service in Visual Studio SDK

Applies to: Visual Studio

This article introduces common causes and solutions when you can't get a service in the Visual Studio SDK.

If the requested service can't be obtained, the call to GetService returns null. Always test for null after requesting a service:

IVsActivityLog log =
    GetService(typeof(SVsActivityLog)) as IVsActivityLog;
if (log == null) return;

The service isn't registered with Visual Studio

Examine the system registry to see whether the service has been correctly registered. For more information, see How to: Provide a service.

The following sample .reg file fragment shows how the SVsTextManager service might be registered:

[HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio\<version number>\Services\{F5E7E71D-1401-11d1-883B-0000F87579D2}]
@="{F5E7E720-1401-11d1-883B-0000F87579D2}"
"Name"="SVsTextManager"

In this example, version number is the version of Visual Studio, such as 12.0 or 14.0, the key {F5E7E71D-1401-11d1-883B-0000F87579D2} is the service identifier (SID) of the service, SVsTextManager, and the default value {F5E7E720-1401-11d1-883B-0000F87579D2} is the package GUID of the text manager VSPackage, which provides the service.

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

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 won't be found if:

  • An interface type is passed to GetService instead of the service type.
  • No GUID is explicitly assigned to the interface. Therefore, the system creates a default GUID for an object as needed.

The VSPackage requesting the service hasn't been sited

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.

The wrong service provider is used

Be sure that you're 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 doesn't know about SVsRunningDocumentTable. You can call GetGlobalService to get a VSPackage service provider from within a tool window.

If a tool window hosts a user control or any other control container, the container will be sited by the Windows component model and won't have access to any Visual Studio services. You can call GetGlobalService to get a VSPackage service provider from within a control container.

References