10 commands you don't want to be without in .Net Core
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
- 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
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:
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:
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
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:
Here are some references you might need:
.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