为什么我无法调试动态加载的程序集? [英] Why am I unable to debug a dynamically loaded assembly?

查看:84
本文介绍了为什么我无法调试动态加载的程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个Web API项目,该项目使用内部的模拟框架,该框架允许拦截和修改来自控制器的响应.它使用MEF加载一个程序集,该程序集包含在某些前提条件匹配的情况下执行的代码.

I am working on a Web API project that uses an in-house mocking framework that allows to intercept and modify the responses from the controllers. It uses MEF to loading an assembly that contains code that is executed if some preconditions are matched.

我知道这是正常工作的,因为我可以在响应中看到模拟已执行,但是由于某种原因,我无法调试动态加载的程序集中的代码.尽管断点看起来很活跃,但执行从未中断过.

I know that this is working properly, because I can see in the response that the mock has been executed, but for some reason I am unable to debug the code in the dynamically loaded assembly. Although breakpoints look active, execution never breaks there.

我尝试调用Debugger.Break();,它确实中断了,但是调用堆栈显示为空,而Visual Studio仅显示此消息:

I tried calling Debugger.Break(); and it indeed breaks, but the call stack appears empty, and Visual Studio only shows this message:

我可以看到程序集及其符号已加载到模块窗口中:

I can see that the assembly and its symbols are loaded in the modules window:

我能够在调用动态加载的程序集(behavior参数)之前中断,如下所示:

I am able to break just before the call to the dynamically loaded assembly (the behavior parameter), which looks like this:

private HttpResponseMessage ApplyBehavior(
    IMockBehavior behavior,
    string controller, string action,
    HttpRequestMessage request,
    HttpResponseMessage mockedResponse)
{
    return behavior.Apply(controller, action, request, mockedResponse);
}

如果我尝试在立即窗口中检查behavior变量,Visual Studio将显示以下异常:

If I try to inspect the behavior variable in the immediate window, Visual Studio shows the following exception:

behavior.GetType()
'behavior.GetType()' threw an exception of type 'System.IO.FileNotFoundException'
    Data: {System.Collections.ListDictionaryInternal}
    FileName: null
    FusionLog: null
    HResult: -2147024894
    HelpLink: null
    InnerException: null
    Message: "Cannot load assembly 'SuperMam.WebAPI.Mocking'."
    Source: null
    StackTrace: null
    TargetSite: null

这是一个相当大的应用程序的一部分,我无法提取相关部分.我试图收集尽可能多的信息,但仍然不知道为什么会这样.

This is part of a fairly large application, and I am unable to extract the relevant parts. I tried to gather as much information as I could, but still have no clue on why this is happening.

我该怎么做才能解决此问题?

What can I do to fix this problem?

可以肯定的是,如果我从控制器中调用该代码,则可以正常进入该代码:

Just to be sure, if I call the code from my controller, I am able to step into it normally:

var behaviourType = AppDomain.CurrentDomain.GetAssemblies()
    .First(a => a.FullName.Contains("SuperMam.WebAPI.Mocking"))
    .GetType("SuperMam.WebAPI.Mocking.MyBehaviour");

var behavior = (IMockBehavior)Activator.CreateInstance(behaviourType);
// I can step into this, and view the behaviour variable in the watch
behavior.Apply("dummy", "dummy", Request, null);

但是即使我这样做,当模拟框架调用相同的方法时,我也无法介入.

but even when I do this, when the same method is called by the mocking framework, I can't step into it.

我还注意到,同一程序集(FullName是相同的)被加载了两次.这两个实例之间的区别在于它们的CodeBaseLocation属性:

I also noticed that the same assembly (FullName is identical) is loaded twice. The difference between the two instances is their CodeBase and Location properties:

  • 其中之一的CodeBase等于我的应用程序的bin目录,而Location等于C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\...
  • 其他人的CodeBase等于第一个人的Location(C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\...),而Location是一个空字符串.
  • One of them has CodeBase equal to my applications's bin directory, and Location equal to C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\...
  • The other one of them has CodeBase equal to the first one's Location(C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\...), and Location is an empty string.

这可能是问题的原因吗?

Could this be the cause of the problem?

推荐答案

事实证明,原因是我未想到MEF正在加载程序集.正在使用Assembly.Load(File.ReadAllBytes(mockDllPath))加载它.由于程序集是从字节数组加载的,因此调试器没有调试信息.

Turns out that the reason was that the assembly was not being loaded by MEF as I thought. It was being loaded using Assembly.Load(File.ReadAllBytes(mockDllPath)). Since the assembly was loaded from a byte array, no debugging information was available to the debugger.

这篇关于为什么我无法调试动态加载的程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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