Developing a .Net core console application on Linux
The latest version of .Net also known as .Net core is now cross platform with support for Linux and Mac OSX in addition to Windows. I’ve been planning to try this out from some time now, and with the latest version of .Net core RC2 out I decided to give this a try.
I’ll be covering the steps to develop and run a simple .Net core console application that prints a simple Hello World on the screen.
I tried this out on Ubuntu 14.04 LTS distribution. However, the steps are almost similar on any other flavour of Linux. Most of it also holds true for OSX too.
Firstly download the latest version of Visual Studio Code.
This is the latest cross platform editor from Microsoft.
With this, we can now open the Linux terminal and check if the .Net core is installed properly.
To do this, run the following command.
For .Net core RC2 installed on Ubuntu 14.04, the output would be as follows…
You can also check the options available with the new dotnet command line tool.
Output:
We can now proceed towards developing the cross platform console application.
Create a new folder to create your application and navigate to it.
Use the dotnet commandline tool to create the console application.
This will create the following files in the project folder
The Program.cs file is similar to the file that we had in the earlier .Net frameworks.
It has the main method which serves as the entry point for the console application.
The project.json file is similar to the csproj file that were part of the earlier .Net projects.
This has configuration details like the .Net core framework version being used, the dependent assemblies being used etc.
The dependent/referenced assemblies are all Nuget packages which are downloaded on the fly from Nuget respositories. (This is somewhat similar to the Maven respository system that we have in Java)
It is essential that these are downloaded before we can run the application.
To do this run the following on the terminal…
Output:
Output:
Now that the compilation has succeeded, we may now run the console application using
Output:
With this we have now successfully created a console application on Linux using the new cross platform .Net core libraries.
Leave a Comment