runtime: Debugger cannot break in custom AssemblyLoadContext

I’m working on transitioning from .NET Framework to .NET Core 2.1, and I’ve hit a snag when it comes to replacing AppDomains. I need to load script code on the fly, and usually this is done in a separate process. This turned out to be pretty clunky for debugging purposes, so I use a separate code path that loads the script into an AppDomain instead while I’m working on it. Setting breakpoints etc worked just fine the separate AppDomain, but the equivalent in .NET Core (using AssemblyLoadContexts) isn’t working.

(Side note: I don’t really care that I can’t unload the scripts until .NET Core 3.0 comes out, as I said it’s only for debugging.)

Here’s a minimal example:

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Loader;

namespace TestLoadContext
{
	class Program
	{
		internal class MyAssemblyLoadContext : AssemblyLoadContext
		{
			internal MyAssemblyLoadContext() { }
			protected override Assembly Load(AssemblyName assemblyName) => null;
		}

		static void Main(string[] args)
		{
			Console.WriteLine("Hello from main context!");

			var loadContext = new MyAssemblyLoadContext();
			var assembly = loadContext.LoadFromAssemblyPath(typeof(Program).Assembly.Location);
			var startMethod = assembly.GetType(typeof(Program).FullName).GetTypeInfo().GetMethod("Run");
			startMethod.Invoke(null, new object[0]);
			Debugger.Break();
		}

		public static void Run()
		{
			Console.WriteLine("Hello from custom context!");
			Debugger.Break(); // <--- DOES NOTHING
		}
	}
}

If I try to step into the Run method, I get a VS error: “Unable to step. Operation not supported. Unknown error: 0x80004005”

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 18 (5 by maintainers)

Most upvoted comments

The loader context issue fix is in Visual Studio 2019 Preview 2 (https://blogs.msdn.microsoft.com/visualstudio/2019/01/24/visual-studio-2019-preview-2-is-now-available/). Can you give it a try and let us know if it resolves the issue?

Tested now with this repro and works like a charm. Great news. Thanks.

Thanks @per-samuelsson! Closing this issue.

The loader context issue fix is in Visual Studio 2019 Preview 2 (https://blogs.msdn.microsoft.com/visualstudio/2019/01/24/visual-studio-2019-preview-2-is-now-available/). Can you give it a try and let us know if it resolves the issue?

Happy 2019! 😉

@tommcdon Could you please share when this fix is going to be available in Visual Studio?

+1, would be neat to have it fixed. Thanks! 👍