Search code examples
c#visual-studiovisual-studio-extensionsvisual-studio-2019

VS2019 extension using IDebugEventCallback2


I created a new VS extension using VS2019 16.1.6. and I added this using statement

using Microsoft.VisualStudio.Debugger.Interop;

and added the interface IDebugEventCallback2 to my class

public sealed class VSIXProject1Package : AsyncPackage, IDebugEventCallback2

Not I get the error:

error CS0433: The type 'IDebugEventCallback2' exists in both 'Microsoft.VisualStudio.Debugger.Interop, Version=8.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'Microsoft.VisualStudio.Debugger.InteropA, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

How can I get rid of this error? Or is there an other way to react to debugger events than using IDebugEventCallback2?

edit: Problem reported to Microsoft: https://developercommunity.visualstudio.com/content/problem/651199/vs2019-extension-using-idebugeventcallback2.html


Solution

  • VS2019 uses PackageReference format to manage nuget packages for VSIX project.

    And by default it will reference Microsoft.VisualStudio.SDK and Microsoft.VSSDK.BuildTools package.Also, since Microsoft.VisualStudio.SDK package have dependencies on many other packages, this project will also reference those packages.

    See this simple structure:

    Microsoft.VisualStudio.SDK
       ......(other dependencies)
       --Microsoft.VisualStudio.Debugger.Interop
          --Microsoft.VisualStudio.OLE.Interop
       --Microsoft.VisualStudio.Debugger.Interop.10.0
          --Microsoft.VisualStudio.Debugger.InteropA
       ......(11.0,12.0,14.0,15.0)
       --Microsoft.VisualStudio.Debugger.Interop.16.0
          --Microsoft.VisualStudio.Debugger.InteropA
    

    So it's clear this issue results from the VSIX project adds reference to both Microsoft.VisualStudio.Debugger.Interop and Microsoft.VisualStudio.Debugger.InteropA. These two assemblies have the same namespace Microsoft.VisualStudio.Debugger.Interop, and all have IDebugEventCallback2 Interface. I think it's why causes this issue.

    As a workaround:

    Normal we can use extern alias for this situation. But it hasn't supported for PackageReference format yet. Fortunately I found a good hint from gertjvr. So all we need is:

    Unload the project=>Edit the xxx.csproj=>Add the content below into the project file:

      <Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
        <ItemGroup>
          <ReferencePath Condition="'%(FileName)' == 'Microsoft.VisualStudio.Debugger.Interop'">
            <Aliases>signed</Aliases>
          </ReferencePath>
        </ItemGroup>
      </Target>
    

    You can change the ReferencePath to Microsoft.VisualStudio.Debugger.InteropA if you want to use the Interface from this assembly. It depends on your need.