Visual Studio 2017 - 更新NuGet包后,VSIX扩展程序停止工作

4

复现步骤:

  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.

1个回答

2
有很多方法可以解决这个问题,这里介绍其中一种:

enter image description here

  • 删除旧的packages.config文件。
  • 删除所有现有的引用(请参见下图)。
  • 手动编辑.csproj文件,删除包含“..packages…”内容的所有行,包括检查某些Nuget存在性以及相关导入和任务的所有行。
  • 作为Nuget包添加Microsoft.VisualStudio.Shell.15.0Newtonsoft.Json。现在您应该看到类似于此的内容(请注意带有蓝色图标的新Nuget引用):

enter image description here

  • 现在您应该能够成功编译和运行。

几个月来,每当我遇到无法解决的Nuget问题时,都会采取这种方法。我将旧的packages.config复制到其他地方,然后将其从项目中删除,记录“根”包,删除每个引用,检查csproj文件,并从Nuget添加新引用(尽可能添加最少数量以确保子引用正常)。现在,无论我们是否喜欢,Nuget都是所有引用的未来,甚至包括Visual Studio。


天啊!这个程序跑得真是太好了。谢谢你,好兄弟! - Quark Soup
哎呀!在使用模板添加新命令后,Visual Studio会将所有的垃圾拉回来。看来我现在得忘记使用模板了。 - Quark Soup

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接