Getting started with .NET Core

.NET Core is a collection of libraries and tools that allow to develop multiplatform web apps and services using C# (and many other features https://www.microsoft.com/net/core/platform). In this article we will create and deploy a “Hello World” console app using Visual Studio Code and .NET Core.

Prerequisites

Download and install the .NET Core SDK

We will be using .NET Core 1.1 which is the latest stable version at the time of writing.

Install Visual studio code and these C# extensions:

Please note that and IDE is not necessary to follow the tasks described in this post as we will only need to run commands. I wanted to use Visual Studio Code because it simplifies many things thanks to the embedded terminal and the extensions.

Ok now, let’s create and run a “Hello world” app.

Creating and running a “Hello World’

Create a folder for our first project and open that folder in Visual Studio Code.

Open the terminal view and enter the following command:

dotnet new console -o

The “dotnet” command is included with .NET Core SDK. It can be considered as a swiss knife that allows to do a lot of things like creating and running a project. The “new” argument creates a new project. The “console” argument is the project type that corresponds to a console app. Other project types are available with the “dotnet new” command.

We can notice that the command installs some packages and creates some files in the current folder.

We can notice that the command installs some packages and creates some files in the current folder.

The files created are:

  • A cs file that contains the Main function and some basic code
  • A csproj file that contains project info such as the required packages. This file is compatible VSCode and Visual Studio

After that, run the command

dotnet restore

which will download the packages specified in the csproj

Finally run the app using this command

dotnet run

We can of course modify the source code and the run dotnet restore, dotnet run to execute the new app.

Publishing the app

Now that we have a running app, we want to publish it to our users or customers. The .NET Core SDK provides two ways of publishing depending on whether we want to include the .NET Core libraries or not:

  • Framework-dependent deployments (FDD): build only the app and its dependencies outside of .NET Core. The package is small but the .NET Core runtime should be installed on the target system in order to run the app.
  • Self-contained deployments (SCD): include the .NET Core framework with the binary. This method has the advantage of being fully autonomous. However, we need to build a specific package for each platform we want to target and the target package is relatively heavy due to the size of the .NET Core libraries (around 18 MB).

In the following, we will publish a FDD package and SCD packages for windows 10 and macOS.

FDD

In order to generate a framework dependant binary, open the terminal and run:

dotnet publish -c Release

The tool will generate a dll file in the bin/Release/netcoreapp1.1/publish directory

This dll can be (theoretically) executed on any system that has the .NET Core 1.1 runtime using the command:

dotnet ‘path_to_dll’.

SCD

In order to generate self-contained packages, we first need to specify which platforms we want to target. This is done by adding them in the project file:

In this case, we added the windows 10 x64 and macOS 10.11 platforms. The full csproj looks like this.

Next, run a “dotnet restore” this will trigger the download of the .NET Core libraries that will be included during the publishing step.

In order to publish for a specific platform, we run:

dotnet publish -c Release -r PLATFORM

So, we will run these two commands

dotnet publish -c Release -r osx.10.11-x64
dotnet publish -c Release -r win10-x64

The “dotnet publish” commands generated two folders: bin\Release\netcoreapp1.1\win10-x64\publish and bin\Release\netcoreapp1.1\osx.10.11-x64\publish.

Each one of these folders can run the “hello world” app on any compatible platform autonomously. The main binaries to run are:

Windows -> bin\Release\netcoreapp1.1\win10-x64\publish\project-name.exe

macOS -> bin\Release\netcoreapp1.1\osx.10.11-x64\publish\project_name

You can create a separate zip or setup for each folder and ship it directly to your users or clients. Note that some files can be safely excluded from the folders (like the pdb file).

Running a self-contained app on macOS

Running a self-contained app is extremely easy. I have uploaded a macOS zip generated using SCD method descibed above:

Download and unzip the macOS binary.

Next, open a terminal and cd to the unzipped folder. The last step consists in running the command:

./dotnet_core_tutorials

And that’s it. We have successfully built, shipped and executed a .NET Core app.

Conclusion

Wa have successfully created a multiplatform “hello world” console app without writing a single line of code thanks to the .NET Core SDK. We have also deployed a framework dependant binary (FDD) and two self-contained binaries (SCD).

Of course, .NET Core is not limited to console apps. I vivaciously invite you to explore the full potential of the SDK :).

The source code of this article is on github.

Happy coding ^^

Links:

Show your support

Clapping shows how much you appreciated Yassine benabbas’s story.