Search code examples
c#.netvisual-studio-2022vsix

Visual Studio extension fails when parsing JSON due to not finding "Microsoft.Bcl.AsyncInterfaces"


I am developing a Visual Studio Extension for VS2022. Part of the functionality of the extension involves parsing JSON. For that, I installed the latest version of System.Text.JSON (7.0.1). However, in the experimental instance, when I attempt to run a task that involves parsing JSON, I get this error:

Image of the error

I am using .NET Framework 4.7.2. In the VSIX tab of the project properties, I have it set to deploy the VSIX content to an experimental instance for debugging. Furthermore, my debug configuration looks like this - although I have tried tweaking it to get it to work, to no avail.

enter image description here

I tried a few things. Firstly, I added a reference to Microsoft.Bcl.AsyncInterfaces 7.0.0 from the NuGet package manager. This didn't help. For context, here's what this part of the CSPROJ looks like

  <ItemGroup>
    <PackageReference Include="Microsoft.Bcl.AsyncInterfaces">
      <Version>7.0.0</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.SDK" Version="15.0.1" ExcludeAssets="runtime">
      <IncludeAssets>compile; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.4.2119">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="System.Text.Json">
      <Version>7.0.1</Version>
    </PackageReference>
  </ItemGroup>

I also tried:

  • Resetting the Experimental Instance.
  • Deleting "bin" and "obj" folders in the solution
  • Upgrading to the most recent version of Visual Studio 2022
  • Upgrading System.Text.JSON (I actually was trying it with 7.0.0 before, but I upgraded it to 7.0.1)
  • Bumping the version of the extension

I would appreciate any help you can give to this struggling extension creator :)


Solution

  • The reason for this is that by default Microsoft.Bcl.AsyncInterfaces is suppressed from being included in your VSIX. This is done by the VS SDK in order to reduce duplicate copies of the assembly from being installed (wasting disk space, as VS already includes a version of this assembly, as well as edge case scenarios where loading from not-the-default path can impact VS performance, e.g. breaking NGEN).

    In your case, VS is shipping a copy at version 6.0.0.0, and has a binding redirect from 0.0.0.0-6.0.0.0. However, your extension depends on 7.0.0.0, which can't be located so it fails to load.

    Until VS ships a copy of the 7.0.0.0 version, you'll need to make sure yours is included in your VSIX, and that you provide a code base for it (for example, using the ProvideCodeBase attribute). I think (but haven't verified) that adding this to your project file after the VSSDK.targets import might let it be included in the VSIX:

    <ItemGroup>
      <!-- remove this once VS ships this assembly at the version you need -->
      <SuppressFromVsix Remove="Microsoft.Bcl.AsyncInterfaces.dll" />
    </ItemGroup>
    

    The other option, of course, is to use an older version of System.Text.Json so that it aligns with the version that VS currently ships.