Daniel Cazzulino's Blog : How to get services from the VS DTE (using System.IServiceProvider)

How to get services from the VS DTE (using System.IServiceProvider)

VS is heavily based on a component model and container hierarchy of services (yes, that's most probably where System.ComponentModel came from...). Even though there's no comprehensive documentation of all services available and from which contexts, every now and then I find the need to ask for services, but all I've got at hand is either a EnvDTE.ProjectItem or Project, or just the DTE. So, how do you use the familiar System.IServiceProvider to ask for services?

The trick is to get the latest VS SDK, add a reference to Microsoft.VisualStudio.OLE.Interop.dll and Microsoft.VisualStudio.Shell.dll and use the following simple code:

EnvDTE.Project project; // this is what you have at hand somehow.
 
IServiceProvider serviceProvider = new ServiceProvider(project.DTE as
    Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
 
DynamicTypeService typeService = (DynamicTypeService)
    serviceProvider.GetService(typeof(DynamicTypeService));

The ServiceProvider class in the Shell assembly provides the adapter you need to ask the DTE for services.

posted on Friday, January 06, 2006 9:11 PM by kzu

# Interesting Finds @ Saturday, January 07, 2006 5:15 AM

Anonymous

# re: How to get services from the VS DTE (using System.IServiceProvider) @ Sunday, January 08, 2006 1:34 AM

Hi.
I tried to implement your advice but with no luck.

I try to invoke "Create Connection" dialog from add-in. So I wrote:

System.Array theProjects = (System.Array)applicationObject.ActiveSolutionProjects;
EnvDTE.Project theProject = (EnvDTE.Project)theProjects.GetValue(0);

IServiceProvider serviceProvider = new ServiceProvider(theProject.DTE as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
System.ComponentModel.Design.Data.IDataEnvironment typeService = (System.ComponentModel.Design.Data.IDataEnvironment)serviceProvider.GetService(typeof(System.ComponentModel.Design.Data.IDataEnvironment));
typeService.BuildConnection(null, null);

But I get null reference exception on last line:
typeService is null.
Did I miss something?
Thanks.

Anatoly

# re: How to get services from the VS DTE (using System.IServiceProvider) @ Monday, January 09, 2006 9:38 AM

I never said you were going to get a valid instance of whichever service you happen to ask for. It may very well be the case that the service you're requesting is not available at the DTE level. You must read the documentation for the service in question in order to know who exposes it.

kzu

# re: How to get services from the VS DTE (using System.IServiceProvider) @ Tuesday, March 28, 2006 1:13 PM

Hi,
I'm currently creating a wizard editor for a Guidance Package. This editor should display a list of available types (in current project or referenced) that fulfill some conditions. Actually I tried to use the same editor I use for a component in Windows Forms designer. To make it short, my problem is that the IServiceProvider that I get from the WizardFramework doesn't contain the ITypeDiscoveryService. Do you have some advice how to obtain this service in the GAT environment?

Szymon