Services Reference

Visual Studio exposes functionality through services accessed via IServiceProvider.QueryService(). This reference documents the most commonly used services.

Service Provider Pattern

// Async (recommended)
var service = await ServiceProvider.GetServiceAsync(typeof(SVsUIShell)) as IVsUIShell;

// Sync (legacy)
var service = (IVsTextManager)GetService(typeof(SVsTextManager));

Text Management Services

Service IDInterfaceDescription
SVsTextManagerIVsTextManager, IVsTextManager2Text buffer and view management
SVsCodeWindowIVsCodeWindowCode editor window access
SVsCodeWindowManagerIVsCodeWindowManagerCode window dropdowns and adornments
SVsLinkedUndoTransactionManagerIVsLinkedUndoTransactionManagerLinked undo operations
SVsExpansionManagerIVsExpansionManagerCode snippet expansion
SVsTextSpanSetIVsTextSpanSetText span collections
SVsRegisterFindScopeIVsRegisterFindScopeCustom find scopes
// Example: Get active text view
var textManager = (IVsTextManager)GetService(typeof(SVsTextManager));
textManager.GetActiveView(1, null, out IVsTextView activeView);

Project and Solution Services

Service IDInterfaceDescription
SVsTrackProjectDocumentsIVsTrackProjectDocuments2Track document changes
SVsQueryEditQuerySaveIVsQueryEditQuerySave2Source control edit/save
SVsProjectMRUIVsProjectMRURecent projects list
SVsDesignTimeAssemblyResolutionIVsDesignTimeAssemblyResolutionDesign-time assembly resolution
SVsStartupProjectsListServiceIVsStartupProjectsListServiceStartup projects
// Example: Track document changes
var tracker = (IVsTrackProjectDocuments2)GetService(typeof(SVsTrackProjectDocuments));
tracker.AdviseTrackProjectDocumentsEvents(myEventSink, out uint cookie);

Source Control Services

Service IDInterfaceDescription
SVsSccManagerIVsSccManager2, IVsSccManager3Source control manager
SVsSccToolsOptionsIVsSccToolsOptionsSCC options
SVsRegisterScciProviderIVsRegisterScciProviderRegister SCC providers

Debug Services

Service IDInterfaceDescription
SVsShellDebuggerIVsDebugger, IVsDebugger2, IVsDebugger3Main debugger service
SVsDebugTargetSelectionServiceIVsDebugTargetSelectionServiceDebug target selection
// Example: Check debug mode
var debugger = (IVsDebugger)GetService(typeof(SVsShellDebugger));
DBGMODE[] mode = new DBGMODE[1];
debugger.GetMode(mode);

if (mode[0] == DBGMODE.DBGMODE_Break)
{
    // At breakpoint
}

Shell Services

Service IDInterfaceDescription
SVsUIShellIVsUIShell, IVsUIShell2UI shell (dialogs, windows)
SVsGeneralOutputWindowPaneIVsOutputWindowPaneGeneral output pane
SVsSolutionPersistenceIVsSolutionPersistenceSolution persistence
SVsMonitorUserContextIVsMonitorUserContextF1 Help context
// Example: Show message box
var uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
Guid clsid = Guid.Empty;
uiShell.ShowMessageBox(0, ref clsid, "Title", "Message", null, 0,
    OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
    OLEMSGICON.OLEMSGICON_INFO, 0, out int result);

Component Services

Service IDInterfaceDescription
SComponentModelIComponentModelMEF component model
SVsFrameworkMultiTargetingIVsFrameworkMultiTargetingMulti-targeting info
SVsSettingsManagerIVsSettingsManagerVS settings
// Example: Get MEF service
var componentModel = (IComponentModel)GetService(typeof(SComponentModel));
var myService = componentModel.GetService<IMyMefService>();

UI Services

Service IDInterfaceDescription
SVsThreadedWaitDialogFactoryIVsThreadedWaitDialogFactoryWait dialogs
SVsInfoBarUIFactoryIVsInfoBarUIFactoryInfo bar UI
SVsActivityLogIVsActivityLogActivity logging
// Example: Threaded wait dialog
var factory = (IVsThreadedWaitDialogFactory)GetService(typeof(SVsThreadedWaitDialogFactory));
factory.CreateInstance(out IVsThreadedWaitDialog2 dialog);
dialog.StartWaitDialog("Working...", "Please wait...", null, null, null, 0, false, true);
// ... do work ...
dialog.EndWaitDialog(out bool cancelled);

Build Services

Service IDInterfaceDescription
SVsBuildManagerAccessorIVsBuildManagerAccessorBuild manager access

Best Practices

Async Service Access

// Preferred: async
var service = await ServiceProvider.GetServiceAsync(typeof(SVsUIShell)) as IVsUIShell;

// With Community Toolkit
var service = await VS.GetServiceAsync<SVsUIShell, IVsUIShell>();

Null Checking

var service = GetService(typeof(SVsTextManager)) as IVsTextManager;
if (service == null)
{
    // Handle missing service
    return;
}

Thread Safety

await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));

Error Handling

int hr = textManager.GetActiveView(1, null, out activeView);
if (ErrorHandler.Failed(hr))
{
    // Log or handle error
    return;
}