VSA Scripting in .NET

What is a scripting engine?

A scripting engine is a class that implements the IVsaEngine , as defined in the Microsoft.Vsa namespace. A script engine is capable of loading, compiling, and running script code. A scripting engine must be initialized before it can be used. Once initialized, code items, global items such as object instances, or references can be added to the engine. With my experiences, the order of initialization must occur in the proper sequence in order to avoid errors.

How do I create and initialize the engine?

Creation and initialization of a scripting engine is quite simple. The following steps are a guideline from my experiences that determine the order of operations for initializing a new scripting engine. The order is not concrete, but I have experienced errors when these steps are executed out of the order specified.

1. Creation of the Engine

Once you are ready, and have determined which type of scripting engine you will use, simply create an instance of the desired class. This new instance will be used as your scripting engine. I typically use a variable of type IvsaEngine, but you may use the actual type of engine in your code.

// use the vbscript engine provided in the assembly Microsoft.VisualBasic.Vsa.dll
_engine = new Microsoft.VisualBasic.Vsa.VsaEngine();

    // or

// use the jscript engine provided in the assembly Microsoft.JScript.dll
_engine = new Microsoft.JScript.Vsa.VsaEngine();

2. The Root Moniker

A unique name called a root moniker is used to identify each scripting engine. The root moniker is in the format “ protocol://path” and should be unique to the host. You can use whatever you want as the root moniker, but try naming it something relative to your program. For example, try “yourCompany://yourProgram/language/engineCount”. This will allow easier identification of the engines during debugging and inspection.

// set the root moniker used by the engine
// (ex: "MyApp://MyVsaEngine/Instance#X")
_engine.RootMoniker = rootMoniker;

3. The Site Reference

Each scripting engine will need to communicate with an IVsaSite instance. The scripting engine interface contains a property called “ Site ” that needs to be assigned your implementation of the IVsaSite interface.

// set the site used by the engine to ourself
// as we are the host and will need to communicate with the engine
_engine.Site = this;

4. The Initialization

After the root moniker and site have been set, you must call the InitNew method on the engine. This prepares the engine to have new items added to it. Items such as code items, reference items, or global items. It must also occur before you set the root namespace of the engine.

// readies the engine to add new code items
_engine.InitNew();

5. The Root Namespace

In addition to the root moniker, you will need to specify a root namespace. All types created by the engine will be nested into this root namespace. When the engine runs a script, it will compile the script into an in-memory assembly. You will have access to all of the types created by the script, and the types loaded by the engine. You will need to use the root namespace you specified when the engine was created to gain access to the types you are looking for.

// set the root namespace used by the engine
_engine.RootNamespace = rootNamespace;

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger