为什么我无法调试动态加载的程序集?

Ant*_*bry 12 .net c# debugging mef visual-studio-2015

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

我知道这是正常的,因为我可以在响应中看到模拟已经执行,但由于某种原因我无法调试动态加载的程序集中的代码.虽然断点看起来很活跃,但执行永远不会破坏.

断点处于活动状态

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

应用程序处于中断模式

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

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

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

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

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
Run Code Online (Sandbox Code Playgroud)

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

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

编辑1

只是为了确定,如果我从控制器调用代码,我可以正常进入它:

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);
Run Code Online (Sandbox Code Playgroud)

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

编辑2

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

  • 其中一个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是一个空字符串.

这可能是问题的原因吗?

Ant*_*bry 4

事实证明,原因是 MEF 没有像我想象的那样加载程序集。它正在使用 加载Assembly.Load(File.ReadAllBytes(mockDllPath))。由于程序集是从字节数组加载的,因此调试器无法获得任何调试信息。