Follow me on Twitter, happy to take your suggestions on topics or improvements /Chris
TLDR; this is an article describing 10 great commands in .Net Core CLI, hopefully, there's something for everybody here, from absolute beginner to more seasoned developer.
I come from a long background of doing .Net. One of the best parts of it was Visual Studio, an absolutely outstanding IDE that allowed you to easily write code with code completion, easy debugging and packaging of your apps. Then, years later after I first started with .Net came .Net Core and with it the ability to use .Net cross-platform. With .Net Core came also a very capable CLI tool. Using the old .Net you knew there was an MSBuild command in the background but you seldom needed to care, Visual Studio handled it all for you.
Using .Net Core though you have the best of both worlds, still the first-class experience of either Visual Studio or the amazing Visual Studio Code AND with the CLI tool we have a first-rate terminal experience as well.
So why use terminal commands, I'm a UI guy?
I know many of you are thinking like this (I used to as well) and the answer is that there is more than one reason.
- Scripting is a very good reason, scripting allows us to easily set up CI/CD
- Speed is one factor, working with the terminal is usually faster than using a visual environment.
- Choose your own editor, it also allows you to use the favorite editor of your choice. We all have our favorite editor. .Net used to be very tightly coupled to Visual Studio, now with .Net Core CLI you can quite easily build, test, package, publish your projects through the terminal.
Regardless of the reason you find the most compelling, it's good to know what commands are run underneath and get some insights on what they do for you. So let's look at the 10 most important commands using the .Net Core CLI:
-1- dotnet new
This command helps you scaffold a project. So usually you call it like this:
dotnet new <type of project template> - o <name of directory>
A real example might look like this:
dotnet new console -o app
The above will create a console project in the directory app
. There's a lot more to this command so have a look at the docs page
-2- dotnet restore
This restores the dependencies of a project. What does that mean exactly? Well, when you have added NuGet packages to your solution that adds a reference to your project file, your .csproj
. If you check out the project from GitHub those packages are maybe not versioned and will need to be added to your project. Running restore
at this point will fetch the packages from the NuGet repository.
The command can be run in two ways:
- Explicitly, you type it and it fetches the packages you need
- Implicitly, it runs as part of another command being executed
Here's a full list of commands that runs restore
implicitly:
- dotnet new
- dotnet build
- dotnet build-server
- dotnet run
- dotnet test
- dotnet publish
- dotnet pack
To learn more about this command, read more here:
-3- dotnet build
This command builds a project and all of its dependencies. If you are at the root directory and you have a solution, then it will build the whole solution, so all projects within the solution. If you are in a specific project directory then it will only build that directory, the where matters. There's a lot more to the build
command than that so have a look at some more details at the following link:
-4- dotnet run
The run
command is what you use to execute your code.
To use it simply place yourself in the project directory of the project you want to run and type:
dotnet run
NOTE, It's used in the context of projects, not built assemblies. If you're trying to run a framework-dependent application DLL instead, you must use dotnet
without a command. For example, to run myapp.dll, use:
dotnet myapp.dll
To read more about this command check out the following link:
-5- dotnet test
This command is what you use to execute the tests of a test project. This comes with quite a few arguments which enable you to run specific tests or the whole test suite.
To read more about this command, have a look at the following link:
if you are completely new to testing, check out this article I wrote on getting started with testing:
-6- dotnet pack
Now we are getting into an interesting area namely creating your own NuGet packages. You create a package with this command and you can have packages in a local repository as well as the official global one. To learn more about this have look at the official docs page:
Also, check out an article I wrote about how to do this from scratch and learn how to publish your package so someone else can download it:
-7- dotnet clean
This command cleans the output of a project. This means it takes away both the contents of obj as well as bin folders. This command comes with a few arguments so you can for example choose to only clean a specific runtime or framework. Read more about this command at the official docs:
-8- dotnet sln
This is the command you use to manage your solution. If you are completely new to dotnet, a solution keeps track of many projects and it's great way of managing a group of different projects that logically belongs together to do things like building or maybe publish an executable. Because this command manages everything around solutions it's good to know how to start out.
To create a solution you just place yourself in a directory of your choosing and run
dotnet new sln
This will create a solution file with the same name as the directory. For example:
cd hello
dotnet new sln
This will create a hello.sln
file. To manage the solution you have the following commands at your disposal:
- add, this will add a project to your solution.
- remove, this will remove a project from your solution
- list, this lists all the projects in a solution
To learn more about this command, check out the official docs page:
Additionally, have a look at the blog post I wrote teaching you how to create solutions, projects and how to start out with .Net Core
-9- dotnet add/remove reference
This command will add a project to your project. The purpose is for example for your project to use code from another project. For that to be possible, you first need to add a reference to that project before the compiler and your IDE will recognize it. For example, you are currently developing the console project app
and you want to add the project lib
to app
. You can do this in one of two ways:
-
Add a reference from solution dir, if you are standing in the solution directory (one level above the app directory) then you type
dotnet add app/app.csproj reference lib/lib.csproj
-
Add a reference from the current directory, then you would type
dotnet add reference lib1/lib1.csproj
To learn more about this command, check out the docs page:
-10- dotnet add/remove package
Now we are agin talking about NuGet packages. The add
command allows us to add a package from NuGet by specifying it by name:
dotnet add package Newtonsoft.Json
This will add Newtonsoft.Json
package to our project file. It will also run a dotnet restore
command which will fetch the package from NuGet's repository
To learn more about this command, have a look at this link:
References
Here are some references you might need:
install dotnet
This will take you to the install page for .Net Core. While you are there, check out https://dotnet.microsoft.com. That's a great site for anything .Net, full of tutorials..Net Core Series
Have a look at this series of .Net Core articles I wrote, everything from your first steps in .Net Core, to Serverless and even GraphQL
Top comments (8)
Speed is reason to do CLI?
Well, first of all CLI is good for DevOps and scripting.
.Net developers work in IDE and no reason to use CLI. How often in your project life you do 'dotnet new'? Once.
So difference in 5sec between CLI and ide is nothing..
Similar idea with other commands..
Speed is ONE of the reasons to know and use the
dotnet
CLI. And 5 seconds on a repeatedly executed command (e.g.,build
ortest
) definitely improves the developer experience.Build and test have keyboard shortcuts. Package restore is automatic. Why would I type on command line instead of using an IDE? Just remembered, VS can run tests automatically after build 😃
Personally, I only use the CLI on a day-to-day basis because it provides me with a better development experience (e.g., speed of the commands executed, flexibility in IDE choice, etc.). So if you're very happy with your dev experience, keep rocking on. :-)
However, using the CLI directly improves your knowledge of it, which is irreplaceable if you need to manage the CI/CD pipeline of your application. Setting up some of my first automated builds in TeamCity was difficult for me because I didn't know how Visual Studio was working with command line tools (like 'MSBuild') "under the covers".
well.. back in the day Visual Studio wasn't as fast as it is now.. Yes definitely scripting is a good reason I should have mentioned :) .Net Core developers are a diverse bunch of people. The one on Windows tends to be on Visual Studio. The ones on Mac/Linux (myself included) more sways towards VS Code and other IDEs according to my findings (the not very scientific Twitter polls ;) ). Also you do
dotnet new
once per project, depends on how many projects you have I guess. Anyway appreciate your perspective VitaliyJust to add to this, I'm a Windows user with both full VS and VS code on my laptop. VS code and CLI for the win every day of the week! Full visual studio feels really cumbersome
I can understand that feeling
Good list. As someone currently learning some .NET and coming from the standard web-dev world, I would add:
dotnet watch run
. Basically hot-reloading when you save changes from source (with some caveats).It is now baked into .NET core SDK, although if you are developing your frontend in .NET too and want synchronized browser reloading, you'll also need something like Browser-Sync (guide here)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.