0% found this document useful (0 votes)
22 views

NET Core Overview

Uploaded by

vishwathirty3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

NET Core Overview

Uploaded by

vishwathirty3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

.

NET Core Overview ;


• .NET Core is a new version of .NET Framework, which is a free, open-
source, general-purpose development platform maintained by
Microsoft. It is a cross-platform framework that runs on Windows,
macOS, and Linux operating systems.
• .NET Core Framework can be used to build different types of
applications such as mobile, desktop, web, cloud, IoT, machine
learning, microservices, game, etc.
• .NET Core is written from scratch to make it modular, lightweight, fast,
and cross-platform Framework. It includes the core features that are
required to run a basic .NET Core app.
• Other features are provided as NuGet packages, which you can add it
in your application as needed. In this way, the .NET Core application
speed up the performance, reduce the memory footprint and becomes
easy to maintain.

Why .NET Core?


.NET Core addresses limitations of the .NET Framework by being
open-source, cross-platform, and versatile, enabling development
for various devices and environments with a unified framework.
NET Core is its cross-platform support. Developers can write and deploy .
NET Core applications on multiple operating systems such as Windows,
macOS, and Linux.

.NET Core version history:


1.0 (2016), 2.0 (2017), 3.0 (2019), 3.1 (2019, LTS), 5 (2020), 6 (2021).

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.

.NET Core Characteristics:


1. Cross-platform compatibility.
2. Open-source development.
3. High performance.
4. Modular design.
5. Versatility for various application types.
6. Continuous evolution and updates.
7. Containerization support.
8. Cloud-native capabilities.
9. Simplified deployment process.
10. Enhanced security features.

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.

Why ASP.NET Core?


• Supports Multiple Platforms: ASP.NET Core applications can run on
Windows, Linux, and Mac. So you don't need to build different apps for
different platforms using different frameworks.

ASP.NET Core Version History

ASP.NET Core was introduced in 2016, offering cross-platform capabilities


and improved performance compared to traditional ASP.NET.
ASP.NET 5 onwards, it's unified under the ASP.NET Core framework, retaining
its advantages while accommodating all types of applications.
.NET Core can be installed in two ways:

1. By installing Visual Studio 2022


2. By installing .NET Core SDK (Software Development Kit)

ASP.NET Core MVC Project Structure


We created the ASP.NET Core MVC application. The following is a default project
structure of the ASP.NET Core MVC application in Visual Studio.

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 ;

(CLR) is programming that manages the execution of programs written in any


of several supported languages, allowing them to share common object-oriented
classes written in any of the languages. It is a part of Microsoft's . NET
Framework.

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.

ASP.NET Core framework includes built-in IoC container for automatic


dependency injection. The built-in IoC container is a simple yet effective
container. Let's understand how the built-in IoC container works internally.

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:

1. Framework Services: Services which are a part of ASP.NET Core framework


such as IApplicationBuilder, IHostingEnvironment, ILoggerFactory etc.
2. Application Services: The services (custom types or classes) which you as a
programmer create for your application.

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.

Action Method Injection


Sometimes we may only need dependency service type in a single action method.
For this, use [FromServices] attribute with the service type parameter in the
method.

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 the classic ASP.NET, HttpHandlers and HttpModules were part of request


pipeline. Middleware is similar to HttpHandlers and HttpModules where both
needs to be configured and executed in each request.

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.

Add Custom Middleware ;


In the ASP.NET Core Web API, middleware is a component that sits in the request
pipeline and processes HTTP requests and responses. It allows you to add custom
logic to handle various aspects of the request/response flow, such as
authentication, logging, error handling, and more.

Configure the Default File to be Served on the Root Request


As we learned in the Set Default File section, app.UseDefaultFiles() middleware
serves the following files on the root request.

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

How many ways can you handle exceptions in .NET Core?


In this article I will try to explain various easy ways to handle exceptions:
1. Try Catch block.
2. In Built middleware.
3. Custom Middleware.
4. Exception Filter.
Install Microsoft.AspNetCore.Diagnostics Package
To handle exceptions and display user friendly messages, we need to
install Microsoft.AspNetCore.Diagnostics NuGet package and add middleware in
the Configure() method.

The Microsoft.AspNetCore.Diagnostics package includes following extension methods to


handle exceptions in different scenario:

1. UseDeveloperExceptionPage ;

The major purpose of UseDeveloperExceptionPage() is to help the user to


inspect exception details during the development phase.

2. UseExceptionHandler ;

UseExceptionHandler is a middleware method that is used to handle


exceptions that are thrown during the processing of an HTTP request in an
ASP.NET Core application.

Serving Static Files


Static files, such as HTML, CSS, images, and JavaScript, are assets an ASP.NET
Core app serves directly to clients by default.

ASP.NET Core application cannot serve static files by default. We must


include Microsoft.AspNetCore.StaticFiles middleware in the request pipeline.

Logging in .NET Core


.NET supports high performance, structured logging via the ILogger API to help
monitor application behavior and diagnose issues. Logs can be written to different
destinations by configuring different logging providers. Basic logging providers are
built-in and there are many third-party providers available as well.

How does logging work in .NET core?


Basics of the . NET Core Logging With LoggerFactory. It is designed as a logging API
that developers can use to capture built-in ASP.NET logging as well as for their own
custom logging. The logging API supports multiple output providers and is extensible to
potentially be able to send your application logging anywhere.

Logging in ASP.NET Core ;


Logging is the process of recording events in software as they happen in real-time,
along with other information such as the infrastructure details, time taken to
execute, etc. Logging is an essential part of any software application.

.NET Core Application Types


We can create two types of applications in .NET Core.

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.

Configure Application Type ;

Configure Application TypeWe can configure ASP.NET Core application


as portable or self-contained application using type property of
Microsoft. NETCore. App dependency in project.
Code Sharing in .NET Core ;
ASP.NET core uses new SDK-style file projects which means we can easily open
any file and make changes to the file again and again. For sharing code in the
controller we will create one <ItemGroup> tag. Then after we will add the reference
to the old project file to the new project file.

Target Multiple Frameworks in .NET Core 2.x


App
As mentioned in the previous chapter, creating a .NET Core application which
targets multiple frameworks is one of the approaches for code sharing.

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.

You might also like