Introducing C# script runner for .NET Core and .NET CLI

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:

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

You can now execute it using:

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:

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

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.

Be Sociable, Share!

  • Igor Oleinikov

    Great! Thank you.
    It’d be nice to be able to create that project.json by dotnet new -t script.

  • Paul Wheeler

    Awesome work!