Extensibility Architecture API

Extensibility Architecture API , ANGULAR WITH CORE .NET AND MVC RAZOR ENGINE Extensibility Architecture API

 Extensibility Architecture API

This chapter will explain some of the essential aspects of ASP.NET Core from an
extensibility point of view. We will learn more about hosts and servers, as well as about
creating a custom server. We will revisit the concept of middleware, delving deeper into
more advanced scenarios. As mentioned before, RESTful APIs deliver resources located
on a specific URI, which can be driven by routing. We will also learn more about routing
from a practical standpoint. In the last section of this chapter, we will cover hosting
services, which are used for running concurrent background tasks within an ASP.NET
Core application.

Hosts and Servers

The concepts of hosts and servers play a vital part in the RESTful architecture. We
can implement a host with a server that listens for remote requests by clients in ASP.
NET Core, and it will beautifully align with the Client-Server constraint dictated by the
principles of REST.

We briefly touched on the web host in the previous chapter. In ASP.NET Core, a
host is responsible for bootstrapping, initialization, and lifetime management of
applications. For a web application to run, it requires a host with at least one server for
serving requests and responses.

Part of the bootstrapping responsibility is setting the proper configuration for the
application. The IWebHostBuilder provides a built-in functionality to configure the
application configuration model and the application services as well as a mechanism to
set and get settings that are key/value-based.




Creating a Custom Server

To create our custom server, we simply need to implement IServer, which has the
following signature:

public interface IServer : IDisposable
{
    IFeatureCollection Features { get; }
    Task StartAsync<TContext>(IHttpApplication<TContext> application,
    CancellationToken cancellationToken);
    Task StopAsync(CancellationToken cancellationToken);
}


Middleware

The components that are assembled within the application request pipeline are called
middleware and are responsible for handling requests and responses passing through
the pipeline. In the context of an HTTP request pipeline, middleware is also commonly
referred to as request delegates, which are orchestrated using the Run, Map, and Use
extension methods.





Middleware as a separate class is preferred over inline middleware in ASP.NET Core.
Writing middleware as classes require a class with at least one constructor that expects
the next RequestDelegate in the pipeline as a parameter and an Invoke method that
receives the HttpContext. Regarding the rest of the code changes, it was pretty much
copy-and-paste, except when invoking next(...), as it required the HttpContext as a
parameter


public static class NumberCheckerMiddlewareExtensions  // This is extension method
{
    public static IApplicationBuilder UseNumberChecker(this IApplicationBuilder app)
    {
        return app.UseMiddleware<NumberCheckerMiddleware>();  //This is  Middleware
    }
}


After above changes, we need to make below change

public void Configure(IApplicationBuilder app)
{
    app.UseNumberChecker();
}


ASP.NET Core comes with many standard middleware options built-in for
authentication, CORS configuration, response caching and compression, routing, managing
sessions, static files, and managing URL rewriting. In the next section, we will look at how
the routing middleware can help us when working with URLs in our application.

Routing

Request routing is one of the fundamental features of any API application and helps
with identifying resources as Uniform Resource Identifiers (URIs). In ASP.NET Core we
make use of the RouterMiddleware, which gives us the functionality to map an incoming
request to a route handler

Application routes are configured when the application starts up, and they can
optionally extract values from request URLs to be used further along in the pipeline.
Because the routes are defined based on a specific template, it is also possible to
generate URLs for particular routes during runtime.

An application uses one collection of routes, and when a request comes into the
handler, it scans each route in order and matches the given request to the appropriate
template.

To start using the routing functionality, we follow the Add/Use pattern to opt in to the
routing feature within ConfigureServices of the application Startup:

public void ConfigureServices(IServiceCollection services)
{
services.AddRouting();
}

Hosted Services

A hosted service provides a mechanism for running background tasks within the lifetime
scope of the application. It is quite handy in scenarios where long-running tasks need to
run in the background of an application continuously.
ASP.NET Core provides the IHostedService interface for creating hosted services.
Let’s take a look at its signature.

public interface IHostedService
{
    Task StartAsync(CancellationToken cancellationToken);
    Task StopAsync(CancellationToken cancellationToken);
}


We need to implement the above interface in order to use Hosted Service

With ❤️ Happy Coding

Comments

Popular posts from this blog

How to disable table row in Angular

How to create XML file dynamically in c#.net?

How to get Array value in Angular ?