Getting Started With .Net Core Command Line Tooling

.Net Core, Cross Platform

0 ( 0 votes) | 1305

In this article, we will talk about how to get started with .Net Core Command Line Tools. So let`s get started.

What is .Net Core?

It is a general purpose cross platform development platform supported by Microsoft.

Characteristics:

  1. Cross Platform
  2. Provides command line tools.
  3. Open Source
  4. Supported By Microsoft.

In this article, we will talk about the command line tooling provided by .Net Core.

Getting Started:

.Net Core 1.0 command line tool chain is a cross platform tool which allows us to develop .Net core applications using command line interface. It`s first RTM release was announced on June 27, 2016. It is the primary layer on which other higher level tools are built. We can use the .Net core command line interface on Windows, Mac and various flavors of Linux including RedHat. For more information on supported opearating system click here. Commands are same on each supported platform. Means even if I use Mac machine to learn the command line tooling, I can use the same commands on Windows or Linux machine and continue learning.

How To Install .Net CLI Tooling:

It`s very easy to install .Net Core Command Line Tooling. Just open up the browser and enter http://dot.net/core URL. It will take us to the page where we can download the installer according to the operating system and install .Net Core Command Line Tooling. I am using the windows machine but you can follow along and execute these commands on any supported operating system.

After installation, to verify that it`s installed properly just open up the command prompt (Shortcut: WIN+R and type cmd) and type dotnet and hit enter. You will be presented with following information (F: 1).

.Net Core

To get the information about the current version of the command line tooling, type the following command and hit enter.

dotnet --version

Output of above command is as shown below (F: 2):

.Net Command Line Tooling Version

As we can see in above image, the current installed version is 1.0.0-preview2-003131. Multiple versions of the command line tooling can be installed side by side. Location for the installed versions on windows machine is C:\Program Files\dotnet\sdk (F:3).

.Net Core SDK Installed Location On Windows MachineAs we can see in above image, I have two versions installed on my machine. Latest version which was also displayed when we run the dotnet --version command (Refer F: 2) is highlighted in red color in above image.

Now let`s run following command in command prompt to get the list of command that we can execute.

dotnet --help

 Output of above command is as shown below (F: 4).

.Net Core Available Commands

As we can see in above image there are total 7 commands which are listed at the bottom of the image along with their description that we can execute. In this article, we will focus three main commands:

  1. dotnet new - Initialize a new project
  2. dotnet restore - Restore the dependencies specified in .Net Project
  3. dotnet run - Run the program

The first command is new command using which we can create a basic .Net project. To get more information about the new command, type the following command in command prompt and hit enter.

dotnet new --help

Output of above command is as shown below (F: 5)

.Net New Command Options

As we can see in above image, using the -l option we can specify the language of the project. Currently only C# and F# languages are supported. Visual Basic support is coming soon. Using -t option we can specify the type of the project. 

Currently four types of projects are supported:

  1. Console
  2. Web (ASP.NET Core Web Application)
  3. Lib (Class Library)
  4. Xunittest

If we don’t specify the values for -l and -t parameters explicitly while executing the dotnet new command, then default language is C# and default project type is Console application.

Creating First Hello World Console Application Using Command Line Tooling:

To create the first Hello World console application using command line tooling, let`s first create a folder called AspnetCore using the following command on any hard drive.

mkdir AspnetCore

Type the following command to switch to our newly created directory.

cd AspnetCore

Let`s again create a new folder called HelloWorld using following command.

mkdir HelloWorld

Type the following command to switch to HelloWorld directory.

cd HelloWorld

After that type the following command and hit enter.

dotnet new or dotnet new -l C# -t Console

Output of above command is as shown below (F: 6):

.Net Core Console Application Is Created Using dotnet new command

As we can see in above image, we have successfully created a new HelloWorld console application. Type dir command and hit enter to see the numbers of files in our new project. Output of dir command is as shown below (F: 7).

.Net Core Console Application Project Files

As we can see in above image, our new project just contains two files:

  1. Program.cs
  2. project.json

We can use any editor of our choice to edit these files. 

Let`s use Visual Studio Code which is a cross platform editor which works on Windows, Linux and Mac OS. Type the following command in command prompt and hit enter to open the HelloWorld project in Visual Studio Code.

code .

Once the project is open in visual studio code, double click on Program.cs file to open it. You will see some red squiggly lines and a popup message from Visual Studio Code that “There are unresolved dependencies from project.json. Please execute the Restore command to continue” as shown below (F: 8).

.Net Core Application Restore Message in Visual Studio Code

We can click on Restore button as shown in above image or execute dotnet restore command in command prompt to download the dependencies from nuget. Even if we click on Restore button in Visual Studio Code, behind the scenes it will execute the dotnet restore command for us and will restore the dependencies.

Output of restore command is as shown below (F: 9)

.Net Core Restore Command Output

Once the dependencies are resolved, red squiggly lines will get removed. Now the final step is to run the program. We can run the program using dotnet run command. To execute it, we can either go back to our command prompt or Visual Studio Code also has integrated command prompt which we can open using Ctrl+` shortcut key. Once the command prompt is open, type the dotnet run command to execute the application.

Output of above command is as shown below (F: 10)

.Net Core Console Application Output

As we can see in above image Hello World message is printed out to console. Great we have successfully created a console application using dotnet core command line tooling and executed it.

Reference:

1. https://docs.microsoft.com/en-us/dotnet/articles/core/

Conclusion:

In this article, we learned about how to get started with .NET Core Command Line Tooling and created a basic Hello World console application. I hope you enjoyed reading the article.

Happy Coding!

Manoj Kulkarni - Dotnetcontext

Manoj Kulkarni

I am programmer, passionate blogger and foodie. I live in Nashik. I am a .Net developer. I like to learn new technologies to keep my self-updated and share that knowledge with my blog readers, friends and colleague.

2 Comments


Tushar - Dotnetcontext

TusharFriday, November 4, 2016 5:58 AM

Nice article !! ... waiting for web application article in .net core ...

Manoj Kulkarni - Dotnetcontext

Manoj KulkarniFriday, November 4, 2016 5:59 AM

Thank you for the feedback Tushar.

Add a new comment