大网数据广告活动
Web服务器与PHP

Can't get a service in Visual Studio SDK

· 52CE 我爱测

Introduces common causes and solutions when you can't get a service in the 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 xref:Microsoft.VisualStudio.Shell.Package.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, xref:Microsoft.VisualStudio.Shell.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 xref:Microsoft.VisualStudio.Shell.Package.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 xref:Microsoft.VisualStudio.Shell.Interop.STrackSelection, but doesn't know about xref:Microsoft.VisualStudio.Shell.Interop.SVsRunningDocumentTable. You can call xref:Microsoft.VisualStudio.Shell.Package.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 xref:Microsoft.VisualStudio.Shell.Package.GetGlobalService* to get a VSPackage service provider from within a control container.

References

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors (upstream author metadata: aartig13). Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 11/04/2016
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。