| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
You can reference other scripts from your CSX script by using a #load directive.
Here is an example:
#load "models.csx"
#load "service.csx"
using System;
using ServiceStack.WebHost.Endpoints;
using System.Reflection;
public class AppHost : AppHostHttpListenerBase {
public AppHost() : base("StarterTemplate HttpListener", Assembly.GetExecutingAssembly()) { }
public override void Configure(Funq.Container container) {
Routes
.Add<Hello>("/hello")
.Add<Hello>("/hello/{Name}");
}
}
var port = "http://*:999/";
var appHost = new AppHost();
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.
#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.
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.
Here is an example:
#load "setup.csx"
#r "nunit.core.dll"
#r "nunit.core.interfaces.dll"
var path = "MyUnitTests.dll";
var runner = TestSetup.GetRunner(new[] {path});
var result = runner.Run(new ConsoleListener(msg => Console.WriteLine(msg)), TestFilter.Empty, true, LoggingThreshold.All);
Console.ReadKey();This allows you to use yours or third-party compiled .NET assemblies within your script.
#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 your CSX file there and the #r directive will be correctly recognized.
You can pass arguments to your scripts using -- as a delimiter. For example:
scriptcs my.csx -- arg1 arg2
In the script, arguments can be access by inspecting the Env.ScriptArgs collection, which itself is an IReadOnlyList<string>.
Console.WriteLine("Argument one is {0}", Env.ScriptArgs[0]);
Console.WriteLine("Argument two is {0}", Env.ScriptArgs[1]);
Console.WriteLine("Argument count is {0}", Env.ScriptArgs.Count);
| Back | FazBrowse Home | New Git URL |