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
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.
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.
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.
After leaving the service host the request is travels in the pipeline as a HttpRequestMessage. These are the handlers used in the Lifecycle.
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.
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.
The URI works as a ModelBinderParameterBinder object that checks that there are custom IModelBinder an IValueProvider.
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.
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.
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 executes the Action Method code. And it generates the HttpResponseMessage.
Now we see the image of the Result Conversion:
- 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.
TOP No#1 Recommended ASP.NET Hosting
ASPHostPortal.com is the leading provider of Windows hosting and affordable ASP.NET MVC Hosting. ASPHostPortal proudly working to help grow the backbone of the Internet, the millions of individuals, families, micro-businesses, small business, and fledgling online businesses. ASPHostPortal has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch, ASPHostPortal guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability.