left-icon

ASP.NET Web API Succinctly®
by Emanuele DelBono

Previous
Chapter

of
A
A
A

CHAPTER 3

The Life of a Request

The Life of a Request


Processing a request

When a client sends a request to an ASP.NET Web API application, there are three layers that process the request. The main components that play an active role in this route are shown in the following figure:

Steps in Request Processing

Let’s see what happens at each stage.

Down the rabbit hole

Consider a request that is issued from a client and reaches the first layer.

The Hosting Layer

The first layer is the hosting layer, which receives the HTTP request directly from the client. The hosting layer could be a classic Internet Information Server that uses the ASP.NET pipeline, or a self-hosted application (we will talk more about self-hosting in Chapter 11).

The role of the hosting layer is to receive the requests and convert them into instances of HttpRequestMessage, a class that represents the request. This request message is passed down to the Message Handler Pipeline. How this request is built depends on the hosting type, but for now we won’t go any further.

The Message Handler Pipeline

The message handler pipeline represents the middleware of our architecture. It consists of a chain of handlers that are pluggable to meet the needs of the application. Each handler is an instance of a class derived from HttpMessageHandler that has one method, SendAsync, which receives an instance of HttpRequestMessage and returns an HttpResponseMessage.

Each of these handlers has a reference to an InnerHandler, the next handler in the chain that will be called in sequence.

With this architecture, each request can be pre-processed or post-processed by multiple handlers doing different things.

Examples of message handlers are the HttpRoutingDispatcher that dispatches the request based on the route and the HttpControllerDispatcher that sends the request to the controller.

These handlers are already on the chain, as they are in the collection HttpConfiguration.MessageHandlers. Others can be added to the collection during the configuration of the Web API application.

The two handlers that we previously mentioned are the two special handlers at the end of the chain.

Controller Handling

We are now at the bottom of the route. The controller handling layer receives the request message from the layer above it and calls the action on the controller passing the required parameters. The task is accomplished by the HttpControllerDispatcher, the last handler in the chain. This, with the help of HttpControllerDescriptor, obtains an instance of a class that implements the IHttpInterface and calls the method ExecuteAsync on this instance. Selecting the correct action to execute is the job of the ApiController.ExecuteAsync method, which binds the parameters, executes the action filters (if they are present), and executes the action itself.

An IActionResultConverter converts the result of the action to an instance of HttpResponseMessage. The response message goes up to the client following the same path as the request.

Summary

In this chapter we took a quick look at the main components that compose the ASP.NET Web API and the role of these components. In the next chapters we will go inside each of these modules to understand how they are implemented and how we can use them to build our applications.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.