Search code examples
visual-studio-2017vsixvs-extensibilityvsixmanifest

Visual Studio 2017 - VSIX Package stops working after updating NuGet packages


Steps to reproduce:

  1. Open Visual Studio 2017. Select File.. New.. Project..
  2. From the New Project dialog, select Extensibility.. VSIX Project..
  3. Select the new Project from the Solution Explorer. Right Click and Add.. New Item.. Extensibility.. Custom Command..
  4. You now have a complete custom command built from the standard templates. Run it in the debugger. You'll get a experimental instance of Visual Studio. From the menus, select Tools... Invoke Command1... Your custom command works!
  5. Hit 'OK' and exit the experimental instance of Visual Studio.
  6. Now, back in the original solution, click Tools.. NuGet Package Manager.. Manage NuGet Packages for Solution...
  7. In the Updates tab, you'll see there are many updates to the standard packages included in the template. Select them all and update them. Repeat until all your packages have been updated. You will likely need to restart VS during the process.
  8. Build your VSIX package again. You may have to supress the warning around this statement:

    public static async Task InitializeAsync(AsyncPackage package)
    {
        // Verify the current thread is the UI thread - the call to AddCommand in Command1's constructor requires
        // the UI thread.
    #pragma warning disable VSTHRD109 // Switch instead of assert in async methods
        ThreadHelper.ThrowIfNotOnUIThread();
    #pragma warning restore VSTHRD109 // Switch instead of assert in async methods
    
        OleMenuCommandService commandService = await package.GetServiceAsync((typeof(IMenuCommandService))) as OleMenuCommandService;
        Instance = new Command1(package, commandService);
    }
    
  9. Debug your VSIX package again.

  10. Select Tools... Invoke Command 1...
  11. You get an error: The 'Command1Package' package did not load correctly. enter image description here
  12. If you follow the instructions to the %AppData% directory and read the activity log, you'll see that the Microsoft.VisualStudio.Threading 15.8.0.0 assembly can't be loaded.

    Could not load file or assembly 'Microsoft.VisualStudio.Threading, Version=15.8.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.Threading, Version=15.8.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.


Solution

  • There are many ways to fix this (mess). Here is one

    enter image description here

    • delete the legacy packages.config file.
    • remove all existing references (see the picture below)
    • edit the .csproj manually and remove all lines that contain "..packages..." stuff, including all lines that check for some nuget presences, and also the associated imports and tasks.
    • add Microsoft.VisualStudio.Shell.15.0 and Newtonsoft.Json as a nuget packages. You should now see something like this (note the new nuget references with the blue icon):

    enter image description here

    • you should be able to compile and run successfully

    For a few months now, everytime I face impossible nuget issues, that's what I do. I copy the old packages.config somewhere, delete it from the project, remember the "root" packages, remove every reference, check the csproj, and add the new references from nuget (trying to add the fewest possible so the child references are ok). Nuget is the future for all references now (we'll have to bite the bullet whether we like it or not), even for Visual Studio ones.