In this post I wanted to share a little project I’ve been working on recently – a .NET CLI and .NET Core based script runner for C# (CSX scripts).

The idea was super simple – I just wanted to be able to author C# scripts using .NET Core, leverage project.json to define the script dependencies and execute scripts cross platfom using .NET CLI – via a dotnet script command.

The project is located here on Github. You can head over and have a look at readme to get started – but, briefly, the key features are listed here.

dotnet script is entirely self contained

You do not need to install anything globally if you don’t want. Because the script runner itself is developed as .NET CLI tool, it can be referenced from with a project.json – the same file you’d define the dependencies for your script – and restored using dotnet restore.

A typical C# script project.json file would look like this:

{ "dependencies": { "Automapper": "5.1.1", "Newtonsoft.Json": "9.0.1" }, "frameworks": { "netcoreapp1.0": { } }, "tools": { "Dotnet.Script": { "version": "*", "imports": [ "portable-net45+win8", "dnxcore50" ] } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { "dependencies" : { "Automapper" : "5.1.1" , "Newtonsoft.Json" : "9.0.1" } , "frameworks" : { "netcoreapp1.0" : { } } , "tools" : { "Dotnet.Script" : { "version" : "*" , "imports" : [ "portable-net45+win8" , "dnxcore50" ] } } }

After running dotnet restore, the runner + all dependencies get restored, allowing you to write a C# script, for example:

using Newtonsoft.Json; using AutoMapper; Console.WriteLine("hello!"); var test = new { hi = "i'm json!" }; Console.WriteLine(JsonConvert.SerializeObject(test)); Console.WriteLine(typeof(MapperConfiguration)); 1 2 3 4 5 6 7 8 9 using Newtonsoft . Json ; using AutoMapper ; Console . WriteLine ( "hello!" ) ; var test = new { hi = "i'm json!" } ; Console . WriteLine ( JsonConvert . SerializeObject ( test ) ) ; Console . WriteLine ( typeof ( MapperConfiguration ) ) ;

You can now execute it using:

dotnet script {path-to-file.csx} 1 dotnet script { path - to - file . csx }

dotnet script runs cross platform

Your scripts are effectively speaking small netcoreapp1.0 apps – they can reference any .NET Core packages compatible with it, and run on any platform supported by it.

dotnet script supports debugging

You can debug scripts executed with dotnet-script in Visual Studio Code. You can read the detailed instructions on how to get that set up here.

You’ll be able to set up breakpoints in the script code and step through it.

dotnet script supports code evaluation (file-less)

You can pass snippets of C# code directly to the runner and have it evaluate it for you without a need of accessing a physical file on disk.

For example:

dotnet script eval Console.WriteLine(\"Hi\"); 1 dotnet script eval Console . WriteLine ( \ " Hi \ " ) ;

This is roughly equivalent (and inspired by) to Node.js’s:

node -e console.log(\"foo\") 1 node - e console . log ( \ " foo \ " )

dotnet script is compatible with C# Interactive

The runner is compatible with Roslyn’s own script runner – csi.exe (C# Interactive), which is part of VS Tooling. It does not attempt to introduce its own scripting dialect and exposes the same global properties – for example Args to access script arguments – as CSI.

If you’d like to help – see you over at Github! Special thanks already to Atif and Adam, as well as to Bernhard.