ASP.NET Core 2.0 Fundamentals and beyond

Key things you need to know about ASP.NET Core & What's new in version 2.0

Vlad

Vlad

Software Development Engineer at Softvision
For more than five years, Vlad has been working with the Microsoft stack (web, cloud, desktop) on different projects. He's passionate about new cool technologies, frameworks, and tools that appear every day.
Vlad

Latest posts by Vlad

ASP.NET Core in a Nutshell

We can create applications based on .NET Framework or .NET Core. Each framework runs on different operating systems. .NET Framework runs only on Windows machines. On the other hand, .NET Core is cross-platform, modular and it’s based on NuGet components.

ASP.NET Core can run on both .NET Framework or .NET Core, while ASP. NET 4.6 runs only on .NET Framework. (see Figure 1)

 Figure 1.

 History of ASP.NET

  • 1996 – ASP (Active Server Pages): Introduced running on VB script and on IIS
  • 2002 – NET: Web framework based on the first version of .NET
  • 2008 – NET MVC: Lightweight, highly testable presentation framework
  • 2012 – NET WebAPI: Framework for building web APIs (REST calls) on top of the .NET Framework

What is ASP.NET Core?

ASP.NET Core is an open-source, cross-platform web framework. It’s considered to be the next generation of ASP.NET. It was announced in 2014 and released in 2016 – version 1.x and in 2017 – version 2.0. It’s based on NuGet packages. and runs on the full .NET framework on Windows or on the cross-platform .NET Core framework. ASP.NET Core was developed by Microsoft and the community.

Why use ASP.NET Core?

ASP.NET Core has a lightweight, high-performance and modular HTTP request pipeline. It’s open-source and has the ability to build and run on Windows, macOS, and Linux. It has built-in dependency injection and can be easily integrated with modern client-side frameworks and development workflows (e.g. AngularJS, KnockoutJS and Bootstrap).

Setting up ASP.NET Core

To setup ASP .NET Core we first need to install .NET Core. Just open a web browser and type ‘dot.net. Based on the type of operating system, install the corresponding SDK. To verify if .NET Core has been installed correctly,  just open a console window or terminal and type ‘dotnet‘ or ‘dotnet —help’. You should see something similar like in Figure 2:

 Figure 2.

 Project structure

When creating a new project in Visual Studio (File > New Solution > .NET Core > ASP.NET Core Empty), the resulting project structure will look similar with Figure 3. It contains the project dependencies: NuGet ( AspNetCore.All metapackage ) and SDK. The “launchSettings.json”  file contains the defined profiles and environment variables for running the application.
The “wwwroot” folder allows serving of  files (CSS, JS, images, etc.)

Figure 3.

Program.cs file ( see Figure 4.)

The ‘WebHost.CreateDefaultBuilder‘ call is responsible for creating a web application host.  A Kestrel web server is automatically allocated. After building and running the web host, the application is hosted and it begins listening to HTTP requests.

Figure 4.

Startup.cs file 

The Startup class contains 2 main methods: “ConfigureServices” and “Configure“. 

ConfigureServices method (see Figure 5)
This is where we define our services for our application ( e.g. MVC, EF, Identity ). A service is a component used for common consumption by the application. The services are made available through dependency injection.

Figure 5.


Configure method (see Figure 6)
This is where we define our middleware in the request pipeline. Think of ‘middleware‘ as a piece of code that performs asynchronous logic. Basically, when a request comes to a middleware, if it can’t handle the request, then it invokes the next middleware in the pipeline. If it can handle the request, it terminates the request and returns a response for the request.

Figure 6.

What does ‘Middleware’ mean?
Given the HTTP request pipeline, we can add middleware components to process the incoming request. In Figure 7 the request comes through Middleware 1 to Middleware 3 and comes back in Middleware 1, generating the response.

If there is nothing in the pipeline (no middleware is present), nothing happens.

It’s important to note that in the Configure method, Middleware 1 is the first middleware code then followed by Middleware 2 code and finally by Middleware 3.

Reordering the middleware code for the same request will give us possible different responses.

In essence, middleware is a pluggable component that can handle the request or gives control to the next middleware in the pipeline.

Figure 7.

ASP.NET vs. ASP.NET Core

What’s new in version 2.0? 

  • Faster ( + 25% than version 1.x)
  • Metapackage – The Microsoft.AspNetCore.All metapackage contains:
    • All supported packages by the ASP.NET Core team.
    • All supported packages by the Entity Framework Core.
    • Internal and 3rd-party dependencies used by ASP.NET Core and Entity Framework Core.
  • Runtime Store
    • contains all the runtime assets needed to run ASP.NET Core 2.0 applications
  • Simplified Application Host Configuration
    • The Program.cs file has been simplified as against to previous versions (1.x)
  • Angular and React templates
    • New project templates have been added in the .NET core CLI for creating out-of-the-box Angular or React .NET Core projects

Performance

In Figure 8, we can see how ASP.NET Core performs in comparison to ASP.NET 4.6 and NodeJS.

Given the same performance server and load params with CPU at 100%, ASP.NET Core is approximately three times faster than NodeJS and approximately six times faster than ASP.NET

Figure 8.

Share This Article


Vlad

Vlad

Software Development Engineer at Softvision
For more than five years, Vlad has been working with the Microsoft stack (web, cloud, desktop) on different projects. He's passionate about new cool technologies, frameworks, and tools that appear every day.
Vlad

Latest posts by Vlad

No Comments

Post A Comment