ASP.NET Core Tutorial

In this course of ASP.NET Core, we will cover all the basic, intermediate, and advanced ASP.NET core concepts that help you build data-driven web applications by the end of this course you will be able to perform all the CRUD operations that are Create, Read, Update and Delete using Sequal server as our database. 

The topics which we are going to cover in detail are-

  • ASP.NET Core.
  • ASP.NET Core MVC.
  • ASP.NET Core Identity.
  • Entity Framework Core.

What is ASP.NET Core?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-based, Internet-connected applications. ASP.NET Core is a redesign of ASP.NET 4.x and earlier it was named ASP.NET 5 and later is renamed ASP.NET Core.

Few benefits and features of ASP.NET Core.

Cross-Platform: ASP.NET Core applications can be developed and run across different platforms like Windows, macOS, Linux. ASP.NET Core applications can be hosted on IIS, Apache, Docker, and even Self-host in your own process.

Because of One Unified Programming Model for MVC and Web API, both the MVC Controller class and the ASP.NET Web API Controller class inherit from the same Controller base class and returns IActionResult.

ASP.NET Core has great build-in support for Dependency Injection which is a very great feature to learn.

ASP.NET Core Testability feature makes unit testing easy for developers.

It is an Open Source technology so it is continuously growing and improving with the support of developers from the open-source community.

Modular: ASP.NET Core provides modularity with Middleware Components. Both the request and response pipelines are composed using the middleware components. A rich set of built-in middleware components are provided out of the box. Custom Middleware Components can also be created.

Middleware - Is a piece of software that can handle HTTP request or response

Prerequisites for this course:

  • Basic understanding of HTML, CSS, and C#.
  • Prior MVC knowledge is helpful but not required. 

ASP.NET Core Project File.

.csproj or .vbproj depending on the programming language used.
No need to unload the project to edit the project file.
File or Folder references are no longer included in the project file.
The File System determines what files and folders belong to the project.

TargetFramework.
Specifies the target framework for the application.
To specify a target framework we used Target Framework Moniker (TFM)

AspNetCoreHostingModel.
Specifies how the application should be hosted.
InProcess or OutofProcess.
InProcess hosts the app inside of the IIS working process (w3wp.exe)
OutProcess hosting model forward web requests to a backend ASP.NET Core app running the Kestrel server. The default is OutofProcess hosting.

PackageReference.
Used to Include a reference to the NuGet package that is installed for the application.
Merapackage - Microsoft.AspNetCore.App
A metapackage has no content of its own. It just contains a list of dependencies (other packages).
When the version is not specified, an implicit version is specified by the SDK. Rely on the implicit version rather than explicitly setting the version number on the package reference. 

Main method in ASP.NET Core.
A Console application usually has a Main() method.
Why do we have a Main() method in ASP.NET Core Web Application?
ASP.NET Core application initially starts as a Console application.
This Main() method configures ASP.NET Core and starts it and at that point it becomes an ASP.NET Core web application.


The Main() method called "CreateWebHostBuilder" and we are passing the command line argument to it. We can notice that "
CreateWebHostBuilder" return "IWebHostBuilder" and "Build()" is called on that which build the webhost on which ASP.NET Core Application and on this we add "Run()" and start listening the incoming Http request.

We are also configuring the "Startup" class using the extension method named ".UseStartup"
Going to the definition of "Startup" class we find two important method "ConfigureService" configure the service required for our application and "Configure" configure our application request processing pipeline 

Some of the Tasks that CreateDefaultBuilder() performs.
Setting up the web server. Loading the host and application configuration from various configuration sources and Configuring logging.

An ASP.NET Core application can be hosted.
  • InProcess or 
  • OutofProcess.

ASP.NET Core InProcess Hosting.

To configure InProcess hosting.
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>

CreateDefaultBuilder() method calls UseIIS() method and host the app inside of the IIS worker process (w3wp.exe or iisexpress.exe)

InProcess hosting delivers significantly higher request throughput than OutOfProcess hosting 

To get the process name executing the app.
System.Diagnostics.Process.GetCurrentProcess().ProcessName

With out-of-process hosting.
2 Web Servers - Internal and External Web Server.
The internal web server is Kestrel.
The external web server can be IIS, Nginx, or Apache.

What is Kestrel?
Cross-Platform Web Server for ASP.NET Core.
Kestrel can be used, by itself as an edge server.
The process used to host the app is dotnet.exe.


Dependency Injection in ASP.NET Core.

What is Dependency Injection?
Dependency Injection (DI) is the technique to achieve IOC (Inversion of Control). Inversion of Control means we need to make our code loosely coupled so in the future if there are any changes then there should not be any tight coupling between any two classes of object.


This is the basic structure of our project when we are not using Dependency Injection and we are using some service(class) inside one or more Controller then we create an object of the service using the "new" keyword and suppose in the future if we make any changes like changing the name of the service then we have to make those changes inside all Controllers. This is an example of tightly coupling.


Now let's understand what happens when we work with Dependency Injection, suppose I have only one service so to work with dependency injection I need to create one interface of my service. Now I will not work directly with the service I will use an interface to create this field and to store the object of this particular service. Now if we are making any changes in service then we don't have to make any changes in the control which is using it.

Now one important question that may come to our mind that where I will create the instance of the service and how the application will come to know that this interface is resolved by using this particular class. ???

In the concept of dependency injection, we generally create a container, and inside that container, we define some settings. 

How to configure Dependency Injection in ASP.NET Core?


ASP.NET Core provides the built-in support for DI. Dependencies are registered in containers and the container in ASP.NET core is IServiceProvider. Services are typically registered in the application's Startup.ConfigureService method.

Service Lifetime.
Service can be registered with following lifetimes-
  • Transient (AddTransient<>) - A  new instance of the service will be created every time it is requested.
  • Scoped (AddScoped<>) - These are created once per client request. For each HTTP request
  • Singleton (AddSingleton<>) - Same instance for the application.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS