Simplified Hosting Model is a specific implementation of the "type sharing" pattern for for 2-way host-script communication scenarios. When some script is executed according this model it has access to all loaded assemblies of the host application (including the host application itself). This model is enabled by default but can be disabled if required by setting CSScriptLibrary.CSScript.ShareHostRefAssemblies to false.
Simplified Hosting Model offers huge benefits for both compiling and loading stages of the script execution by placing the responsibility for location the required referenced assemblies on the script engine . The following is a typical script hosting sample, which follows Simplified Scripting Model:
Samples\Hosting\HostingSimplified\Hello.cs
using System; using CSScriptLibrary; public class Host { public static string greeting = "Hello World!"; static void Main() { AsmHelper scriptAsm = new AsmHelper(CSScript.LoadCode( @"using System; public class Script { public static void SayHello() { Console.WriteLine(Host.greeting); } }")); scriptAsm.Invoke("Script.SayHello"); } } |
Referencing external assemblies
It is actually hard to imagine a scenario when Simplified Hosting Model would be insufficient. That is why all hosting samples (Samples\Hosting) are following the Simplified Hosting Model. In a way this model is a "one size fits all" answer for all hosting scenarios. Newer the less, it is possible that your script needs to access some types which are implemented in an external assembly, which is not a dependency assembly of the host application and does not belong to GAC. Host.cs script from Samples\Hosting\Referencing is an example of such runtime scenario. It accesses the type ExternalClass implemented in ExternalAsm.dll and this assembly is not loaded by the host application (Host.cs).It is important to remember that when you host the script engine the script execution is not much different from the standalone script execution (from command-prompt). Thus when executing the the script from application you can express script dependencies (on some external assemblies) in the standard way through "//css_reference" and "using" statements (see Using .NET Assemblies).
However when hosting the script engine you have an additional referencing mechanism, which can be more convenient in some cases. Almost all script compiling/loading methods of the AsmHelper class can accept path to the referenced assemblies. Thus you can explicitly indicate which assemblies the script is referencing from the host code.string refAssembly = Path.GetFullPath("ExternalAsm.dll"); string compiledScript = CSScript.Compile(script, refAssembly); |