Introduction
This article mainly describes the framework for running ASP. NET Web APIs, that is, the process from receiving an HttpRequest to the corresponding process.
Runtime framework
The API framework consists of three layers: hosting (host), message handler (message processing pipeline), and controller handler (controller processor)
Hosting host
The bottom layer is responsible for the web api host, for example, this interface is in the Web API and HTTP Runtime. Simply put, this layer is responsible for creating HttpRequestMessage instances and pushing them to the previous layer of Message Processing pipelines. At the same time, this layer is also responsible for returning HttpResponseMessage to the MPs queue.
Generally, two existing hosting options are available: SelfHost and WebHost.
SelfHost is based on WCF. It replaces the Message with HttpRequestMessage and pushes it to the upper-layer Message processing pipeline.
WebHosting converts HttpRequest to HttpRequestMessage Based on HttpControllerHandler.
The WebAPI host is not limited to the two, but also contributed by some communities, such as Louis Dejardin's OWIN and the author's host, based on Azure Service.
Message Handler Pipeline Message processing Pipeline
The middle layer is composed of a message processing pipeline, similar to an existing WCF Web API. This pipeline exposes an HttpServer class, which also extends the HttpMessageHandler (combination mode ).
This pipeline provides some scalability points, such as logs and user authentication.
There is a special handler at the upper layer of the pipeline: HttpControllerDispatcher. This handler is responsible for obtaining and calling a controller for this request.
HttpControllerDispathcer is used only when the controller-based programming model controller base-class programming mode (ApiController inherits this class) is used. Normally, we are all encoded based on this mode.
Controller Handling Controller Processor
Finally, the upper layer is equivalent to the controller's processing process, as shown in the following figure:
Action selection
Filter execution
Model binding
Action call
Output content through formatters
The whole process is executed within ApiController and called by HttpControllerDispatcher.
Last
Links are added in many places to provide detailed information. Please leave a message at will.
Original article:
ASP. NET Web API Processing Architecture