次の方法で共有


How to: Troubleshoot services

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

There are several common problems that can occur when you try to get a service:

  • 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:

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 How to: Provide a service.

    The following .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 the example above, 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.

  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.

    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.

See also