简体   繁体   English

使用 Visual Studio 2017 调试 NuGet 包

[英]Debugging NuGet packages using Visual Studio 2017

In Visual Studio 2015 and .NET Core development we could debug NuGet packages by retrieving the source code from a source (eg GitHub) to a local disk, adding the source path to the downloaded source code in global.json and reference the NuGet package in our project.在 Visual Studio 2015 和 .NET Core 开发中,我们可以通过将源代码从源(例如 GitHub)检索到本地磁盘,将源路径添加到global.json下载的源代码并在中引用 NuGet 包来global.json NuGet 包我们的项目。 This caused a reference to the projects in the downloaded source code to automatically be visible in the current solution and thus made it possible to easily debug (More about this functionality can be read about in this article ).这导致对下载的源代码中的项目的引用在当前解决方案中自动可见,从而可以轻松调试(有关此功能的更多信息可以在本文中阅读)。

Does anyone know how to do the same using Visual Studio 2017?有谁知道如何使用 Visual Studio 2017 做同样的事情? Since the global.json is gone I can't find any solution for this.由于global.json消失了,我找不到任何解决方案。

I see this has become a popular question, however, MS is (as for the most time nowadays in Visual Studio) absent in requests that actually can improve their product.我看到这已成为一个流行的问题,但是,实际上可以改进其产品的请求中没有 MS(就目前在 Visual Studio 中的大多数时间而言)。

There are some posts around out there on how to use the reference library from Microsoft, but this doesn't apply to all projects plus you will debug optimized release bits which is limiting in both watch and step capabilities.有一些关于如何使用来自 Microsoft 的参考库的帖子,但这并不适用于所有项目,而且您将调试优化的发布位,这限制了监视和步进功能。 I also feel that this way to do it even slows down a slow Visual Studio even more.我也觉得这样做甚至会进一步减慢 Visual Studio 的速度。 This way to do it is described in this post .这篇文章中描述了这种方法。

However, lately I have found a way to work around this issue.但是,最近我找到了解决此问题的方法。 It isn't always stable, but what can be done is to add the related project to your project as a project reference.它并不总是稳定的,但可以做的是将相关项目添加到您的项目中作为项目参考。

But here are the steps I have done that mostly works:但这里是我所做的最有效的步骤:

  1. Clone the repository of the nuget package from github (or other source)从 github(或其他来源)克隆 nuget 包的存储库
  2. Try your best to find which commit that the nuget package was built from (most projects references with either tags or branches, but don't expect this, it may be better to compare dates on the nuget package and commits).尽力找出 nuget 包是从哪个提交构建的(大多数项目引用带有标记或分支,但不要指望这一点,比较 nuget 包和提交上的日期可能会更好)。
  3. Follow the instructions from the project on how to build it, some is simply building in Visual Studio other may require more steps like using some build scripts in command prompt.按照项目中有关如何构建它的说明进行操作,有些只是在 Visual Studio 中构建,其他可能需要更多步骤,例如在命令提示符中使用某些构建脚本。
  4. Add a reference to the project in the solution, some times you'll also need to add the project that the project references to, but not always.在解决方案中添加对项目的引用,有时您还需要添加项目引用的项目,但并非总是如此。 Haven't found exactly the rules here yet.还没有在这里找到确切的规则。 It seems that newer Visual Studio updates doesn't need this.似乎较新的 Visual Studio 更新不需要这个。
  5. Add a reference to the project in ALL projects that references the nuget package within your solution.在您的解决方案中引用 nuget 包的所有项目中添加对项目的引用。 Failure to do so may cause conflicts which the compiler tries it best (not good enough) to solve.不这样做可能会导致编译器尽力(不够好)解决的冲突。

Build and debug, in your output window check that the assembly located in your project's output folder is used.构建和调试,在输出窗口中检查是否使用了位于项目输出文件夹中的程序集。 If it is, just hit breakpoints in the referenced projects and you'll have full debug functionality.如果是,只需在引用的项目中点击断点,您就会拥有完整的调试功能。

It is a bit of trying and failing to make this work, but it does work eventually.使这项工作有点尝试和失败,但它最终确实有效。

It is possible to create conditions on the project references to ensure that they isn't built in eg release builds, however, be aware that changing configuration requires that you reload your solution AFTER the change!可以在项目引用上创建条件以确保它们不是内置在例如发布版本中,但是,请注意更改配置需要您在更改后重新加载您的解决方案!

I've struggled with this for a long time.我已经为此挣扎了很长时间。 Here's the easiest way that I found that works repeatably:这是我发现可重复使用的最简单方法:

  • Acquire the source code and build a nuget package locally.获取源码并在本地构建nuget包。 You'll need to increment to package version.您需要增加到包版本。 Hopefully the author has provided a .nuspec to make building easier.希望作者提供了一个 .nuspec 使构建更容易。

Create a local nuget source:创建本地 nuget 源:

  • Place the .nuget file that you just created in C:\\Nuget (for example)将刚刚创建的 .nuget 文件放在 C:\\Nuget 中(例如)

  • In Visual Studio, select Tools / Options / Nuget Package Manager / Package Sources在 Visual Studio 中,选择工具/选项/Nuget 包管理器/包源

  • Press the green (+) icon.按绿色 (+) 图标。 Add the path to the local nuget package that you created above (C:\\Nuget) and move the package source to the top of the list.将路径添加到您在上面创建的本地 nuget 包 (C:\\Nuget) 并将包源移动到列表顶部。

  • In your solution, for each project, remove the previous reference to the external nuget package and add the new nuget package from your local nuget source.在您的解决方案中,对于每个项目,删除先前对外部 nuget 包的引用,并从本地 nuget 源添加新的 nuget 包。

  • Start debugging and you should be able to step into the code for the nuget package.开始调试,您应该能够进入 nuget 包的代码。

I'm able to set a breakpoint in the source code from where I build the nuget package and have it break when I run the code.我可以在我构建 nuget 包的源代码中设置一个断点,并在我运行代码时让它中断。

All of the steps above are a little fidgity and you may need to play with it a few times to get it to work.上面的所有步骤都有点烦躁,您可能需要玩几次才能让它工作。

Let me know in the comments if this works for you.如果这对你有用,请在评论中告诉我。 And if so, I'd appreciate an upvote!如果是这样,我将不胜感激! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM