I'm writing an addin for Microsoft Outlook 2007. My goal is to open a work item in a Visual Studio 2010 instance, which I know exists and has the packages loaded for TFS 2012. The following code works fine, until the last line gives me an InvalidCastException:
Imports Microsoft.VisualStudio.TeamFoundation.WorkItemTracking
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.VisualStudio.OLE.Interop
Imports Microsoft.VisualStudio.Shell
Private Sub Test()
Dim vsObj As IServiceProvider = System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0")
Dim sp As New ServiceProvider(vsObj)
Dim docService As DocumentService = sp.GetService(GetType(DocumentService))
... do stuff with docService ...
End Sub
Here is the full exception text:
Unable to cast COM object of type 'System.__ComObject' to class type 'Microsoft.VisualStudio.TeamFoundation.WorkItemTracking.DocumentService'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
I asked the service provider for a DocumentService, and it gave me something, but that something tells me it's not actually a DocumentService. What's the deal? How do I get the DocumentService to know it's a DocumentService, or how do I get something that actually is one?
edit: I've also tried removing "As DocumentService" to get past the invalid cast, but nothing else about the object is discoverable when I inspect it as a Watch. I also tried the following (recommended here), hoping it would shed some light on the problem:
Dim vsObj As IServiceProvider = System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0")
Dim sp As New ServiceProvider(vsObj)
Dim docService = sp.GetService(GetType(DocumentService))
'Note that I still didn't specify a type for docService above.
MsgBox(Microsoft.VisualBasic.Information.TypeName(docService))
To my horror, the message box said "DocumentService", merely reaffirming my indignation. I really have no idea what to do with that.
This related article is the closest I could find to somebody having the same problem, but it was not answered: How to open WorkItem (VS-Team Explorer) from outside visual studio?.
Thank you!