Introduction to .NET Core

Udara Bibile
8 min readJun 21, 2017

Every developer familiar with back-end development have at least heard about .NET technologies. Use of C# with .NET Framework is known as direct competition with Java. In this competition some prefer Java over C# because Java have more mobility where it could be used in Windows, Linux or MacOS. Even though it is feasible to provide Linux and MacOS support to .NET Framework, Microsoft have focused on Windows support and .NET framework is currently run on Windows platform.

Microsoft have finally come up with .NET implementation with cross platform support and it’s called .NET Core. It’s like a stripped down version of .NET Framework carrying only what’s necessary. Most of .NET Core development was done during the last year where v1.0 and v1.1 released in 2016 and v2.0 preview released just few days ago. C# and F# can be used for development.

Light weight .NET Core is known to challenge emerging NodeJS and GO. In benchmarks provided as the release showed significant performance gains over other two. Moreover stable .NET implementation, familiar C# syntax and open source development by Microsoft could be few points to drive in the direction of .NET Core.

“Develop high performance applications in less time, on any platform.” — .NET Core

According to Microsoft future plan it is intended to built up .NET Standard library that would be consistent across .NET Framework, .NET Core as well as Xamarin. .NET Core 1.0 have implemented all .NET Standard 1.0 API where .NET Core 2.0 is expected to align with .NET Standard 2.0 API list.

Let’s get started

Windows installation of .NET Core is quite straight forward. This could be also installed in Ubuntu, Debian and MacOS without much hazel using installation page.

Visual Studio 2017 provides inbuilt support for .NET Core application but there is also nice CLI tools to work with Core. You will be able to check CLI availability through dotnet --version which would reveal installed .NET Core SDK version.

.NET Core is equipped with variety of templates for the developer to get started. dotnet new --help will be useful guide when selecting needed template. Diving into these templates there are templates for console applications, class libraries, unit test projects as well as ASP.NET Core applications (discussed later). Flags could be used to customize templates such where --language f# to use F# instead of C# and --framework netcoreapp1.1 to use v1.1 instead of default v1.0.

Let’s just get started simple console .NET Core application

dotnet new console --framework netcoreapp1.1
dotnet restore
dotnet run

After the first command of dotnet new there will be two files in the directory, namely Program.cs and .csproj files. Varying from earlier Core versions nuget package management was switched to .csproj file instead of .json file, which is similar to use in .NET Framework. So from second command of dotnet restore would restore specified packages in .csproj and built the application. This would create files named obj & bin. obj consists of compiled binary files ready to be linked where bin is the executable code for the application.

Program.cs consists of main function of the application. This console application just consists of Console.WriteLine code for the demo.

Finally dotnet run would run the console application.

What’s ASP.NET Core?

If you’re familiar with .NET Framework it is certain that you have came across ASP.NET too. Simply it’s web framework using .NET Framework and is equipped to work with HTML, and JavaScript. So ASP.NET Core is kind of compact version of ASP.NET and is based on .NET Core rather than .NET Framework.

You may have noticed few ASP.NET Core templates popping up with dotnet new command. We could start with empty ASP.NET Core application using dotnet new web and continue with dotnet restore and dotnet run. Looking into file structure we could see that it’s different from simple .NET Core.

ASP.NET Empty File Structure

Here we can see some additional files such as Startup.cs and directory of wwwroot. If we dive into .csproj file we could see that consists of new package such as Microsoft.AspNetCore which is a dependency for this application.

ASP.NET Empty: Program.cs

In the main function in Program.cs contains code for making server for web applications. KestrelHttpServer developed by ASP.NET Core is being used as cross platform HTTP server. Furthermore wwwroot is set as location for content as well as specify to use Startup.cs.

ASP.NET Empty: Startup.cs -> Configure()

Here Startup.cs should implement Configure() and ConfigureServices() methods.

It is clear that “Hello World!” will be outputted when we navigate to localhost:5000 through web browser.

It is useful to test ASP.NET WebAPI through dotnet new webapi. File structure is somewhat different from ASP.NET Empty template. Notable differences are .json files carrying settings. New package of Microsoft.AspNetCore.Mvc is imported in .csproj while it is included into the application in Startup.cs through ConfigureServices(). This import enable use of Controllers as HTTP endpoints.

Controllers/ValuesController.cs

This implementation with inheritance from Controller enable to manage WebAPI endpoints efficiently. Navigating to localhost:5000/api/values from web browser would return needed output as intended.

Is it possible to use Angular or React?

Being web framework ASP.NET Core should be able to work with SPA (single page applications) to create web pages. Most of back-end services provide their own template tools which would be headache for JavaScript developer. Rather many developers prefers to use popular MV* Frameworks such as Angular or React(with Redux) to manage web pages within ASP.NET Core.

Here ASP.NET Core have developed JavaScriptServices to be used in such instance and provides rich set of templates too. If you are familiar with npm you could just install generator-aspnetcore-spa and use this yeomen generator through yo aspnetcore-spa to choose from variety of JS frameworks.

However it is possible just using Core CLI commands.

dotnet new --install Microsoft.AspNetCore.SpaTemplates::*dotnet new reactredux
dotnet restore // Run 'npm install' for some templates
dotnet run

Above first command of installing Microsoft.AspNetCore.SpaTemplates::* would give access to range of SPA templates.

New templates added

Rest of these commands will generate template supporting react-redux. However some templates require running of npm install to fetch dependency JavaScript files. Navigate to localhost:5000 through web browser to view client template application.

File structure for reactredux template

Need to run NodeJS too?

Luckily ASP.NET Core also provides nuget package to match your need: Microsoft.AspNetCore.NodeServices, enabling run node.js inside server and to use npm packages even at .NET Core run-time.

To use NodeServices in .NET Core server one has to include it through Startup.cs -> ConfigureServices() by services.AddNodeServices(). Developers will be able to use methods such as NodeServices.InvokeAsync to make use of NodeJS properties as needed.

What is Docker support?

You might have noticed about docker installation process give in .NET Core installation page. Simply Docker is a container solution used to avoid any complication in setting up the environment. With probably constructed Dockerfile anyone can make the same environment in any machine, and this is pretty important when working in groups. (Some familiarity with docker is anticipated for following code segments)

Microsoft .NET Core team maintains functioning .NET Core images of many types in the DockerHub. (GitHub like space for Dockerfiles)

Dockerhub

These Dockerfiles contain text commands to properly set up each of these Docker images. Here there are some variations which vary in size and other properties. As an example this repository contains many versions like1.0.5, 1.1.2. Either to select sdk or just runtime. Either to build windows based nanoserver or debian based jessie. Needed Dockerfile should be selected according to the scenario.

Let’s check Docker interactive session to get better understanding.

docker run -it microsoft/dotnet:latestdotnet new console -o myApp
cd myApp
dotnet restore
dotnet run

Docker will get Dockerfile of microsoft/dotnet:latest and will build docker container out of this dotnet image. Here -it would begin interactive session, where it would enter docker container after it is built.

After entering this container it acts as .NET Core installed machine as seen here.

Docker commands like docker ps, docker stop, docker rm will be useful when managing docker containers.

Docker production image out of our application could be made as simple as follows.

dotnet new webapi -o dockerApp 
// Create ASP.NET Core WebAPI application inside ./dockerApp
cd dockerApp
dotnet restore
dotnet run
dotnet publish -c Release -o out

// Production .dll files added to ./out

docker build -t dockerapp .
// Build docker image from 'dockerfile'
docker run -it --rm dockerapp
// Run newly created docker container

Here we could just use microsoft/dotnet:runtime as Dockerfile since at production environment run-time is sufficient enough cause SDK is not used. sdk Docker image will be higher in size while runtimeDocker image will provide required tasks in production environment.

Entity Framework Core too?

.NET Framework surrounding eco-system is always Entity Framework. Microsoft have matched .NET Core also with light weight EF Core to be used for data access. EF Core 2.0 is currently being built to align with .NET Standard as well as .NET Core 2.0.

EF Core is simply ORM (Object Relational Mapper) enabling to work with data as .NET objects. Current EF Core is supported by some relational databases. NuGet package such as Microsoft.EntityFrameworkCore.SqlServer is such connector to SQLServer. EF Core allows querying through LINQ method which is familiar among .NET developers. Code First Migration approach is also feasible with EF Core to setup database.

An interesting feature to look at is EF Core’s InMemory storage found in Microsoft.EntityFrameworkCore.InMemory. But as of now it’s only in testing phase for developers.

According to EF Core 2.1 road-map it’s planning to support SignalR. This will enable real time web capabilities using ASP.NET Core sockets. SignalR is also something to keep in touch since this will provide ability push communication from server to clients.

Some useful GitHub repositories

dotnet-> Core | CoreFX | CoreCLR | Standard |Roslyn | CLI |Docs | Docker

aspnet -> Core | MVC | EF Core | Kestrel | SignalR | Docs

My Code

That’s introductory exposure to .NET Core and its surrounding eco-system. Hope you were able to understand basics of how to get started with .NET Core and feel free to leave comments below. Cheers!!!

--

--