title: Why am I unable to debug a dynamically loaded assembly?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p><a href="https://i.stack.imgur.com/TEau4.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TEau4.png" alt="The breakpoint is active"></a></p>
  
  <p>I tried calling <code>Debugger.Break();</code> and it indeed breaks, but the call stack appears empty, and Visual Studio only shows this message:</p>
  
  <p><a href="https://i.stack.imgur.com/RkaLB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/RkaLB.png" alt="The application is in break mode"></a></p>
  
  <p>I can see that the assembly and its symbols are loaded in the modules window:</p>
  
  <p><a href="https://i.stack.imgur.com/kx4Aa.png" rel="noreferrer"><img src="https://i.stack.imgur.com/kx4Aa.png" alt="Modules window shows that symbols were loaded correctly"></a></p>
  
  <p>I am able to break just before the call to the dynamically loaded assembly (the <code>behavior</code> parameter), which looks like this:</p>
  
  <pre><code>private HttpResponseMessage ApplyBehavior(
      IMockBehavior behavior,
      string controller, string action,
      HttpRequestMessage request,
      HttpResponseMessage mockedResponse)
  {
      return behavior.Apply(controller, action, request, mockedResponse);
  }
  </code></pre>
  
  <p>If I try to inspect the <code>behavior</code> variable in the immediate window, Visual Studio shows the following exception:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>What can I do to fix this problem?</p>
  
  <h1>EDIT 1</h1>
  
  <p>Just to be sure, if I call the code from my controller, I am able to step into it normally:</p>
  
  <pre><code>var behaviourType = AppDomain.CurrentDomain.GetAssemblies()
      .First(a =&gt; 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);
  </code></pre>
  
  <p>but even when I do this, when the same method is called by the mocking framework, I can't step into it.</p>
  
  <h1>EDIT 2</h1>
  
  <p>I also noticed that the same assembly (FullName is identical) is loaded twice. The difference between the two instances is their <code>CodeBase</code> and <code>Location</code> properties:</p>
  
  <ul>
  <li>One of them has CodeBase equal to my applications's bin directory, and Location equal to <code>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\...</code></li>
  <li>The other one of them has CodeBase equal to the first one's Location(<code>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\...</code>), and Location is an empty string.</li>
  </ul>
  
  <p>Could this be the cause of the problem?</p>