NET Core Overview
NET Core Overview
The name .NET Core was changed after .NET Core 3.1. The next version of
.NET Core after version 3.1 was named .NET 5. The latest version of .NET
Core is .NET 7 as of this writing.
.NET 5 unifies the previously separate .NET Core, .NET Framework, and
Xamarin platforms, making it easier for developers to use a single platform
for various application types.
ASP.NET Core ;
ASP.NET Core is the new and totally re-written version of the ASP.NET web
framework. It is a free, open-source, and cross-platform framework for building
cloud-based applications, such as web apps, IoT apps, and mobile backends. It
is designed to run on the cloud as well as on-premises.
Same as .NET Core, it was architected modular with minimum overhead, and then
other more advanced features can be added as NuGet packages as per application
requirement.
This results in high performance, require less memory, less deployment size,
and easy to maintain.
Command-Line Interface ;
The . NET command-line interface (CLI) is a cross-platform toolchain for
developing, building, running, and publishing . NET applications.
The .NET Core CLI is installed with .NET Core SDK for selected platforms. So we
don't need to install it separately on the development machine.
Examples;
• macOS: Mac Terminal.
• Windows: Command Prompt.
• Linux: Linux Bash Shell.
• Google Cloud Platform: PowerShell, Cloud Shell.
• Amazon Web Services: AWS Command Prompt.
• Microsoft Azure: Azure CLI Bash.
The Common Language Runtime ;
Dependency Injection
• ASP.NET Core supports the dependency injection (DI) software design
pattern, which is a technique for achieving Inversion of Control (IoC)
between classes and their dependencies.
• specific to dependency injection within MVC controllers, see Dependency
injection into controllers in ASP.NET Core.
Built-in IoC Container
ASP.NET Core comes with a basic built-in IoC (Inversion of Control) container.
While it supports constructor injection and manages services, it lacks advanced
features found in third-party containers.
If you need features like auto-registration or interceptors, you can replace it with
a third-party container.
The followings are important interfaces and classes for built-in IoC container:
Interfaces:
1. IServiceProvider
2. IServiceCollection
Classes:
1. ServiceProvider
2. ServiceCollection
3. ServiceDescription
4. ServiceCollectionServiceExtensions
5. ServiceCollectionContainerBuilderExtensions
There are basically two types of services in ASP.NET Core:
Constructor Injection
Once we register a service, the IoC container automatically performs constructor
injection if a service type is included as a parameter in a constructor.
For example, we can use ILog service type in any MVC controller.
Property Injection
Built-in IoC container does not support property injection. You will have to use
third party IoC container.
Middleware ;
ASP.NET Core introduced a new concept called Middleware. A middleware is
nothing but a component (class) which is executed on every request in ASP.NET
Core application.
Uses;
Middleware is a powerful tool in . NET Core that allows you to modify incoming
requests and outgoing responses. It can be used to perform a wide range of tasks
such as authentication, logging, compression, and caching. In addition to the built-
in middleware components that are available in
Work ;
The architecture of Middleware in ASP.NET Core consists of a pipeline of
components that handle incoming HTTP requests and outgoing responses.
In ASP.NET Core web applications, the order of middleware execution in the request
pipeline is determined by the sequence in which they are added, allowing for each
middleware to modify HTTP requests and responses and pass control to the next
component.
1. Default.html
2. Default.htm
3. Index.html
4. Index.htm
Environment Variable ;
ASP.NET Core uses the Environment Variable called
ASPNETCORE_ENVIRONMENT, which indicates the runtime environment. The
value of this variable may be something as per the requirement but usually be a
Production, Staging, and Development.
Exception Handling
Exception handling is one of the most important features of any application.
Fortunately, ASP.NET Core includes a middleware that makes exception handling
easy.
exception handling involves catching exceptions that occur during the processing
of requests and returning appropriate error responses to the clients
1. UseDeveloperExceptionPage ;
2. UseExceptionHandler ;
1. Portable Application
2. Self-contained application
Portable Application
Portable applications are applications which expect .NET Core runtime on the
deployment machines. It cannot be run on a machine which does not have .NET
Core runtime installed.
Self-contained Application
Self-contained applications are applications which include .NET Core runtime
when we publish it. It can run on a machine which does not have .NET Core
runtime installed.
We can create .NET Core application and configure multiple target frameworks
for it so that it can run with all the configured target frameworks. To demonstrate
this, let's create .NET Core 2.0 console application which can run with .NET Core
as well as traditional .NET framework in Visual Studio 2017.
Here, we will support two more frameworks . NET Framework 4.0 & 4.6. So include
net40 and net46 monikers respectively as shown below.