从 .net core 2.0 中动态加载的程序集调试代码 [英] Debugging code from dynamically loaded assembly in .net core 2.0

查看:36
本文介绍了从 .net core 2.0 中动态加载的程序集调试代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .net core 2.0 控制台应用程序,它执行以下操作(简化):

I have a .net core 2.0 console app that does the following (simplified):

var a = Assembly.Load(Assembly.GetEntryAssembly()
                              .GetReferencedAssemblies()
                              .First(i => i.Name == "MyAssembly"));

var t = a.GetType("MyType");
var i = (MyBaseType)Activator.CreateInstance(t);

i.Execute();

当我调试代码时,它会按预期进入 MyType.Execute().

When I debug through the code it stepps into MyType.Execute() as expected.

但是,如果我使用以下代码加载程序集:

However If I load the assembly with the following code:

var path = new FileInfo(Assembly.GetExecutingAssembly().Location);
var a = Assembly.LoadFile(Path.Combine(path.DirectoryName, "MyAssembly.dll"));

代码仍然有效,但我在调试时无法进入 MyType.Execute().

The code still works but I can't step into MyType.Execute() while debugging.

知道为什么/出了什么问题吗?

Any Idea why/what's wrong?

推荐答案

这可能是由于应用程序无法找到与 MyAssembly 程序集关联的 PDB 文件造成的,如其中一条评论所述.但是,似乎不需要与程序集位于同一文件夹中的 PDB 文件即可进行调试.

This may be caused by the application not being able to locate PDB file associated with MyAssembly assembly, as mentioned in one of the comments. However, it seems that the PDB file is not required in the same folder as the assembly to make debugging work.

为了检查符号是否加载,请在调用Assembly.LoadFile()后的行中放置一个断点并打开模块 窗口(可以在 Visual Studio 的 DebugWindows 菜单中找到).在此窗口中,找到 MyAssembly 程序集并验证 Symbol Status 列中的值.如果丢失 PDB 是原因,则该值将是无法找到或打开 PDB 文件.".您还可以使用该窗口查看调试器试图找到符号文件的位置.

In order to check if symbols are loaded, please put a breakpoint in the line just after calling Assembly.LoadFile() and open Modules window (it can be found in DebugWindows menu in Visual Studio). In this window, find the MyAssembly assembly and verify value in Symbol Status column. If missing PDB is the cause, the value will be "Cannot find or open the PDB file.". You can also use that window to see where debugger tried to find the symbols file.

调试器在多个位置查找 PDB 文件,如下所述:在 Visual Studio 调试器中指定符号 (.pdb) 和源文件.

Debugger looks for the PDB files in several locations, as described here: Specify Symbol (.pdb) and Source Files in the Visual Studio Debugger.

根据文章,PDB 文件的默认位置是:

According to the article, the default locations for a PDB file are:

  1. 在程序集本身内指定的位置(在编译程序集时由链接器放置在那里)
  2. 动态加载的程序集所在的文件夹
  3. 本地符号缓存文件夹
  4. 互联网、网络或本地符号服务器

我想在你的情况下,应该考虑提到的第一个或第二个位置.

I suppose that in Your case, the first or the second location mentioned should be considered.

另一个需要注意的重要事项是 PDB 必须与程序集完全匹配,因此在重新编译程序集后,还应更新 PDB 文件以匹配新版本.

Another important thing to notice is that the PDB must exactly match the assembly, so after recompiling the assembly the PDB file should also be updated to match the new version.

如果 PDB 文件与程序集匹配并位于上述位置之一,您应该能够调试代码.

If the PDB file matches the assembly and resides in one of the mentioned locations, You should be able to debug the code.

可能还有其他原因,这与使用 .NET Core 没有直接关系,但我认为正确的 PDB 加载可能值得验证.

There may be other causes and this is not directly associated with the fact that .NET Core is used, but I suppose that correct PDB loading may be worth verifying.

这篇关于从 .net core 2.0 中动态加载的程序集调试代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆