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

**URL:** https://forum.alternetsoft.com/t/a-wpf-app-that-loads-c-scripts-dynamically-and-allows-their-debugging-in-a-separate-process/464
**Category:** AlterNET Studio Support
**Tags:** scripter, wpf, editor
**Created:** [July 30, 2024, 11:04am UTC](https://forum.alternetsoft.com/t/a-wpf-app-that-loads-c-scripts-dynamically-and-allows-their-debugging-in-a-separate-process/464 "2024-07-30T11:04:17Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Ihor](https://forum.alternetsoft.com/letter_avatar_proxy/v4/letter/i/606060/32.png) [@Ihor](https://forum.alternetsoft.com/u/Ihor)
#### Post date: [July 30, 2024, 11:04am UTC](https://forum.alternetsoft.com/t/a-wpf-app-that-loads-c-scripts-dynamically-and-allows-their-debugging-in-a-separate-process/464/1 "2024-07-30T11:04:17Z")

</div>

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()`:

```auto
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.WPF` → `DebugRemoteScript`. 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!

---

<div class="post-metadata">

### Author: ![dmitry.medvedev](https://forum.alternetsoft.com/user_avatar/forum.alternetsoft.com/dmitry.medvedev/32/27_2.png) [@dmitry.medvedev](https://forum.alternetsoft.com/u/dmitry.medvedev)
#### Post date: [July 31, 2024, 2:38am UTC](https://forum.alternetsoft.com/t/a-wpf-app-that-loads-c-scripts-dynamically-and-allows-their-debugging-in-a-separate-process/464/2 "2024-07-31T02:38:40Z")

</div>

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](mailto: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
