You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
filipw edited this page Apr 22, 2013
·
6 revisions
Writing a script
Loading referenced scripts
You can reference other scripts from your CSX script by using a #load directive.
Here is an example:
#load "models.csx"
#load "service.csx"usingSystem;usingServiceStack.WebHost.Endpoints;usingSystem.Reflection;publicclassAppHost:AppHostHttpListenerBase{publicAppHost():base("StarterTemplate HttpListener",Assembly.GetExecutingAssembly()){}publicoverridevoidConfigure(Funq.Containercontainer){Routes.Add<Hello>("/hello").Add<Hello>("/hello/{Name}");}}varport="http://*:999/";varappHost=newAppHost();appHost.Init();appHost.Start(port);Console.WriteLine("listening on {0}",port);Console.ReadKey();
This allows you to split your script into smaller maintainable bits or simply place common classes into separate CSX file (and even reuse between different applications). The principles here are exactly the same as including JavaScript files in JS applications.
Usage
#load can only be placed at the top (before first using statement or code line) of your CSX file.
if you place #load inline out script pre-processor will simply exclude it. This is in-line with C# REPL/C# Interactive Window behavior.
#load accepts both relative and absolute paths.
#load is not a scriptcs invention, but rather a C# REPL standard. As a consequence of this, if you have Roslyn CTP installed and you use C# Interactive Window, you can copy paste you CSX file there and the #load directive will be correctly recognized.
Referencing assemblies
Important: If you used scriptcs -install or scriptcs -restore to install packages from Nuget, all relevant assemblies will be available in the script context without having to explicitly reference anything.
You can reference other (loose) assemblies from your CSX script by using a #r directive.
In order for the assembly to be allowed to be used in the CSX file, it needs to be copied into the bin folder relative to the script path or be present in the GAC.
This allows you to use yours or third-party compiled .NET assemblies within your script.
Usage
you can reference assemblies by writing #r <name> and then import the namespace by writing a using-statement.
System, System.Core, System.Data, System.Data.DataSetExtensions, System.Xml, System.Xml.Linq assemblies are referenced by default.
System, System.Collections.Generic, System.Linq, System.Text, System.Threading.Tasks namespaces are referenced by default - explicit using statements for this namespaces aren't needed.
it's not needed to manually reference NuGet packages - they will be loaded automatically (don't forget to scriptcs -install).
#r is not a scriptcs invention, but rather a C# REPL standard. As a consequence of this, if you have Roslyn CTP installed and you use C# Interactive Window, you can copy paste you CSX file there and the #r directive will be correctly recognized.