A WPF app that loads C# scripts dynamically and allows their debugging in a separate process

Hello,

I’m developing a WPF application that dynamically loads C# scripts, where each script is an independent class with its own methods. For example, I have a button that, when clicked, should call method ShowDate():

void ShowDate()
{
    MessageBox.Show(DateTime.Now.ToString())
}

I want to enable debugging for these script methods. Specifically, I’d like to open the corresponding script in a separate process. Set up breakpoint inside of the method ShowDate(). When I click button in my main process, breakpoint should hit. I’ve read about the AttachToProcess debugger functionality which requires running in a different process from the main application.

Currently, I’ve partially implemented the ability to attach the debugger to the main application process when a script method runs. I’m toggling between ScriptRun instances based on the debugging state: using the ScriptRun instance created within the main application process when debugging isn’t needed and switching to a ScriptDebugger.ScriptRun instance created within a remote debugger application process when debugging is active.

The main challenge I’m facing now is passing the instance of ScriptDebugger.ScriptRun from the remote debugger process to the main application process. I tried to follow the example in AlterNet.Studio.AllDemos.WPFDebugRemoteScript. Here’s what I’ve done so far:
Defined the ScriptAPI with a method for remote debugging.
Created the RemoteScriptApi class with two methods:

  • void StartServer(IScriptApi scriptAPI, string? ipcPortName = null);
  • IScriptApi StartClient(string? ipcPortName = null).

The StartServer method is invoked within the remote debugger application process when it starts. The StartClient method is invoked within the main application process after the remote debugger application has been executed.

Could anyone provide any hints, advice, or a helpful demo to guide me in fully implementing this debugging functionality?

Thanks!

Hi Ihor,

The Debug Remote Script demo project seems to be doing just what you need. It uses ScriptRun to compile and run the script if no debugging is needed. In case debugging is required, it compiles the script as an executable and debugs it (by attaching the debugger to the process where the script is running and establishing communication so the script can access application objects).

In order to pass objects between the main process and the process being debugged, the object needs to be “remotable”, and ScriptRun is not.

Also, if your script does not use application objects, there’s no remote communication needed. You can look at the DebuggerIntegration example that shows how to debug application-independent script.

You can send me (dmitry.medvedev@alternetsoft.com) an application where you’re trying to achieve debugging of the scripts that are loaded dynamically so I can have a closer look at it.

Kind regards,
Dmitry