Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 21, 2019 09:23 pm GMT

10 commands you don't want to be without in .Net Core

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. Speed is one factor, working with the terminal is usually faster than using a visual environment. It also allows you to use the favorite editor of your choice. 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

dotnet new command

-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:

dotnet restore command

-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:

dotnet build command

-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:

dotnet run command

-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:

dotnet test command

if you are completely new to testing, check out this article I wrote on getting started with testing:

https://dev.to/dotnet/testing-in-net-core-ojh

-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:

dotnet pack command

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:

Creating your first package in NuGet

-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:

dotnet clean command

-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 hellodotnet 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:

dotnet sln command

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

Starting 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:

  1. 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
  2. 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:

dotnet add reference command

-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:

dotnet add package command

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


Original Link: https://dev.to/dotnet/10-commands-you-don-t-want-to-be-without-in-net-core-40gi

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To