Updated on 2020-01-14
Easily add scripting support to your apps using Microsoft's active script technology
I wanted to take a break from building parsers and such and do something a little different, so here's a handy little library I've whipped up to add scripting support to your .NET applications.
Occasionally, I've found the need to execute JavaScript in my apps, sometimes for scraping, sometimes just to have JS scripting support (VBScript works too) and at least once for legacy ASP emulation.
Microsoft provides COM interfaces to use these but using them from .NET is ugly.
Luckily, this little library makes it easy.
Microsoft provides an extensible scripting framework they use in legacy ASP pages and in Internet Explorer. By default, Windows ships with VBScript and JavaScript (JScript) but 3rd parties have added others.
These script engines are COM visible and expose their scripts as COM objects that can call your code or be called from your code. The only trouble is the interfaces are messy.
What I've done is wrapped these interfaces in two easy to use objects, ScriptHost and ScriptEngine.
First of all, script engines cannot exist autonomously. They need a place to put down roots. That's what ScriptHost is for. ScriptHost manages script engine creation, and lifetime, as well as providing various services to script engines, like error reporting and object retrieval. Before we can create any script engines, we must create one of these lil guys. Remember to dispose of it when you're done. Closing a script host closes all engines that were created by it as well.
Next, we have ScriptEngine which you can get via ScriptHost.Create(). These represent autonomous regions of script code. For an idea of what this means, know that each