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
After the first command of
dotnet new there will be two files in the directory, namely
.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 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.
dotnet run would run the console application.
What’s ASP.NET Core?
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.
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.
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 should implement
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
ConfigureServices(). This import enable use of
Controllers as HTTP endpoints.
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?
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
Above first command of installing
Microsoft.AspNetCore.SpaTemplates::* would give access to range of SPA templates.
Rest of these commands will generate template supporting react-redux. However some templates require running of
localhost:5000 through web browser to view client template application.
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.
NodeServices in .NET Core server one has to include it through
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)
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 like
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
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 rmwill 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
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.
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!!!