4

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!

Community
  • 1
  • 1
user1968292
  • 153
  • 1
  • 7
  • If you make your assignment to a plain old object, then stop there in the debugger and inspect that object, you should be able to deduce the type of the object you're getting back from your call. That might give you some insight into a way to correct the code and avoid the bad cast. – MikeB Jan 10 '13 at 23:23
  • Thanks for such a quick response! When I remove the type specification and inspect "docService", it tells me its type is System.__ComObject, and trying to expand below there gives an error: "No further information on this object could be discovered". Is there a way to interrogate a COM object to ask what interfaces it implements? – user1968292 Jan 11 '13 at 15:32

0 Answers0