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

Β· 440 words Β· 3 minutes to read

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"
      ]
    }
  }
}

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));

You can now execute it using:

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\");

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

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.

About


Hi! I'm Filip W., a software architect from ZΓΌrich πŸ‡¨πŸ‡­. I like Toronto Maple Leafs πŸ‡¨πŸ‡¦, Rancid and quantum computing. Oh, and I love the Lowlands 🏴󠁧󠁒󠁳󠁣󠁴󠁿.

You can find me on Github, on Mastodon and on Bluesky.

My Introduction to Quantum Computing with Q# and QDK book
Microsoft MVP