test

The .Net Core CLI

How to Get Started Using the .NET Core Command Line

A lot has changed in the land of .NET Core since my last blog post.

For me, the best change is the .NET Core tooling. The command line interface (CLI) has really been greatly improved. I’d previously recommended yeoman to setup a new .NET Core project, but now for most scenarios I use the CLI.

The .NET Core CLI

Basic Commands

The .NET Core CLI gives you a quick easy way to do things like:

  1. Start a new project (dotnet new)
  2. Restore package dependencies (dotnet restore)
  3. Build your code (dotnet build)
  4. Run your code (dotnet run)
  5. Package your code for production hosting (dotnet publish)

The CLI works across all platforms, so if you’re doing dev work on Windows, Linux, or Mac all the commands should be the same. This is pretty cool.

Project Stuff

I work with .NET Core almost exclusively through Visual Studio Code (VSCode), again this is cross platform. One thing I almost always forget is that there is a different command to add nuget packages.

In Visual Studio (not VSCode) you can use the Package Manager Console and Install-Package. With the CLI you will use the dotnet add package (details) command to add a package, and the dotnet remove package (details) command to remove a package. The linked docs show how to add a package to a specific project if you’re working in a multi-project solution.

My Workflow

New Project

I like dotnet new for starting up my new projects now. It has a good syntax that allows me to specify the folder to ouput the new project to (via the -o or –output option), and it also now takes a “template” to get you started more quickly. The templates that come pre-installed with the SDK are:

Templates                 Short Name      Language      Tags
----------------------------------------------------------------------
Console Application       console         [C#], F#      Common/Console
Class library             classlib        [C#], F#      Common/Library
Unit Test Project         mstest          [C#], F#      Test/MSTest
xUnit Test Project        xunit           [C#], F#      Test/xUnit
ASP.NET Core Empty        web             [C#]          Web/Empty
ASP.NET Core Web App      mvc             [C#], F#      Web/MVC
ASP.NET Core Web API      webapi          [C#]          Web/WebAPI
Solution File             sln                           Solution

The dotnet new command also provides more options to specify:

  1. The name of the project created
  2. The language to be used (C#/F#)
  3. Framework version
  4. Type of authentication

Non .NET Core CLI Bonus

If you’ve installed VSCode after you setup your new project you can enter code . in your console window and you’ll launch an instance of VSCode to your current working directory. This is pretty handy. This works for me on Windows 10, I’m assuming it will work on other platforms as well.

Building & Running

The .NET Core CLI provides a few commands for building and running your code. The first one you’ll want to run is dotnet restore to restore your Nuget dependencies.

After you’ve got all your packages restored you could run dotnet build and then dotnet run, but using dotnet run by itself will also build your code (if it detects any changes) before running it. Assuming you’re working with a webapi template, running the dotnet run command you’ll see output like this:

c:\core\hello-webapi>dotnet run
Hosting environment: Production
Content root path: c:\core\hello-webapi
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

Building For Deployment

When I’m ready to put code on a server I’ll run dotnet publish, which packs the application and its dependencies into a folder for deployment. The publish command is useful to prep for deployment and other tasks like building a Dockerfile.

A typical publish command will look something like dotnet publish --output dist --configuration release. In this example the publish command specifies the output location and configuration (debug/release) for the code.