Lifecycle of ASP.net Web API Message

Introduction

In this article we will describe the Lifecycle of an ASP. Net Web API message. We know that the ASP. Net Web API is a framework that helps build the services over the HTTP server. It provides the  lightweight services used by multiple clients and devices of the platform to access the data via the API. For transferring the data over the wire , it uses the JavaScript Object Notation (JSON) and Extensible Markup Language (XML)

Now we will define the Lifecycle of an ASP. Net Web API Message from the client to the server and the server to the client. The request is generated by the client using HttpRequest and the return response is generated by HttpResponse.

Diagram of Web API Pipeline

 mp4.jpg

Hosting of web API

We perform two types of hosting on the Web API. They are ASP.Net and Selfhosting, as described in the following:

  • ASP.Net Hosting:  When we perform the ASP.Net hosting then the Lifecycle starts from HttpControllerHeader which is the implementation of IHttpAsyncHandler and the responsibility of it is to send the request to the HttpServer pipeline.
  • Self Hosting:  In self hosting the HttpServer pipeline starts from the HttpSelfHostServer that performs the implementation of HttpServer and the responsibility is that it can be directly listen to the HTTP request.

HTTP Message Handler

After leaving the service host the request travels in the pipeline as a HttpRequestMessage. There is the Handler used in the Lifecycle.

Delegating Handler

This handler is provided to you as a Message of the request before passing onto the rest of the pipeline. The response message can be sent through the delegating handler. And other messages can be monitored and filtered at this point.

Routing Dispatcher

When the request is made using the option Delegation Handler it can be passed to the Routing Dispatcher. The dispatcher checks whether the Route Handler is Null. If the the Route Handler is null then it is passed to the next step in the pipeline.

If it is not null then there are many handlers for routing the message and the desired request is passed to the handler. We select the matching message handler to handle the request.

Controllers

After leaving the service host the request is travels in the pipeline as a HttpRequestMessage. These are the handlers used in the Lifecycle.

Authorization Filter

In the control section the first step is passed to the authorization. If there are Authorization Filters available and the authorization is failed by the request then the request is truncated and sent as a auth failure response.

Model Binding

If the authorization test is successful then it is passed to the model binding section. It is not a single-step process.

If we want to see the model binding process then it looks like
the following.

View of Model Binding

In the model binding we can see the three parts of the HTTP request; they are URI, Headers and Entity Body.

mu1.jpg

URI

The URI works as a ModelBinderParameterBinder object that checks that there are custom IModelBinder an IValueProvider.

Formatter Binding

The Entity body of the model binding is managed by the FrmatterParameterBinding. If there is a request that needs to utilize one or two Media Type Formatters then it is piped through the appropriate Media Type Formatter.

HTTP parameter binding

There is a need for the entire request to be piped through the HTTP parameter binding module, if there is a custom HTTP Parameter Binding module.

Action Filter

After completion of the Model Binding the Action Filter is done. It is invoked two times in the pipeline before the OnExecuting event and after the OnExecution event.

Action Invoker

The action invoker invokes the actions of the control. It binds and models the state for invoking the action in HttpActionContext. It can invoke the action and also return the message in the form of a HttpResponseMessage. If there is an Exception at the time of invoking the action then the exception is routed to the Exception Filter that can send the Error Message.

Controller Action

Controller Action executes the Action Method code. And it generates the HttpResponseMessage.

Result Conversion

Now we see the image of the Result Conversion:

mu3.jpg

  •  HttpResponseMessage: The message is directly passed in it and nothing needs conversion.
  • Void: The action method returns the void result in it and there is a need for conversion into a HttpPesponseMessage.
  •  If the Action Method returns any other type then the Content Negotiation and Media Type Formatters are used. The Content Negotiation checks that we can return the requested content using the appropriate Media Type formatter.

Up Next
    Ebook Download
    View all
    Learn
    View all