How to: Get a Service from the DTE Object

A service can be obtained from any program that has access to the Visual Studio Automation DTEClass object. For example, you can access the SVsActivityLog service from a wizard through the DTE object. You can use this service to write to the activity log. For more information, see How to: Use the Activity Log.

The DTE object implements IServiceProvider, which you can use to query for a service from managed code by using GetService.

To get a service from the DTE object

  • The following code creates a ServiceProvider from the DTE object and calls GetService with the type of the SVsActivityLog service. The service is cast to the interface IVsActivityLog, which is used to write an entry in the activity log. For more information abou how to write to the activity log, see How to: Use the Activity Log.

    
    ' Start with the DTE object, for example: 
    'DTE dte = (DTE)GetService(typeof(DTE)); 
    Dim dte As DTE
    dte = CType(GetService(GetType(DTE)), DTE)
    
    Dim sp As New ServiceProvider(dte)
    Dim log As IVsActivityLog = TryCast(GetService(GetType(SVsActivityLog)), IVsActivityLog)
    If log Is Nothing Then
        Return
    End If
    
    Dim hr As Integer = log.LogEntry(CType(__ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION, UInt32), Me.ToString(), String.Format(CultureInfo.CurrentCulture, "Consuming SVsActivityLog service in {0}", Me.ToString()))
    
    
    // Start with the DTE object, for example:
    // DTE dte = (DTE)GetService(typeof(DTE));
    
    ServiceProvider sp = new ServiceProvider(dte);
    IVsActivityLog log =
       GetService(typeof(SVsActivityLog)) as IVsActivityLog;
    if (log == null) return;
    
    int hr = log.LogEntry(
       (UInt32)__ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION,
       this.ToString(),
       string.Format(CultureInfo.CurrentCulture,
       "Consuming SVsActivityLog service in {0}", this.ToString())
    );
    

See Also

Concepts

Service Essentials

Other Resources

Services