C# – Adding scripting capability to a .NET application

cfossscripting

I already have a working implementation that adds scripting to an application. The application currently generates and compiles scripts from C# snippets (taken from different user provides configuration files) into assemblies using the CodeDOM. As it sounded like a good idea at first I ran into following issues:

  1. difficult AppDomain handling and need for Serialization
  2. performance issue => frequent script changes trigger a recompilation
  3. changes cannot be made in-place in the scripts while running
  4. difficult to provide a GUI for debugging (breakpoints), the script writer actually needs a debugger…

I "hacked" a solution against 1 by loading everything in the main AppDomain. The problem is that nothing can be unloaded… After some time the memory use is huge.

For this reason I eventually would consider to switch to an interpreted solution but want to remain near to a C# language because of the existing code-base. There is a bunch of libs around Vsa, S#, Script.NET, … I need to access objects in my application from the scripts and also run methods in the scripts from my application. Do you also think that switching to an interpreted language the right strategy or should I try to add a debug mode to the existing concept by injecting code like calling a method between each line of the script to allow a step by step execution…. However the AppDomain handling remains a nightmare to handle.

It is certainly not the first time someone runs in such a situation. I could not find any FOSS wrapper/helper library to make the CodeDOM handling easier nor GUI elements to provide building blocks for a minimal debugger. Currently I built ma own GUI and step over calls in the scripts and show changes in the application objects (before/after calls) inside the GUI.

Please only refer to FOSS source/libs.

Best Answer

There are a few interpreted options that are more dynamic but close relatives of .NET. I think I would let my audience drive the choice.

  • For sys-admins I would try and expose powershell commandlets and let them use that. They should be comfortable over there, and it is a very powerful and .NET friendly tool. Credit to @jmh_gr for suggesting this in the comments.

  • For developers I would look at a dynamic CLR implementation of a well-known language. IronPython comes to mind. F# also has an interactive scripting mode that you might be able to use if you want to stick to more official-like languages.

  • For non-developers I would think about using something like Boo to create my own DSL so I could make the verbage make sense.

Finally, none of these strategies are mutually exclusive. In some ways, they build on one another as they require many of the same facilities to be successful.

Related Topic