Antoine Aubry Antoine Aubry - 1 month ago 19
C# Question

Why am I unable to debug a dynamically loaded assembly?

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.

The breakpoint is active

I tried calling

and it indeed breaks, but the call stack appears empty, and Visual Studio only shows this message:

The application is in break mode

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

Modules window shows that symbols were loaded correctly

I am able to break just before the call to the dynamically loaded assembly (the
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);

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

'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"))

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.


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

  • 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?


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.