http://www.c-sharpcorner.com/UploadFile/2b481f/lifecycle-of-Asp-Net-web-api-message/ 1/4 Ask a Question ARTICLE 25 4,392 In thi s arti cl e we wi l l descri be the Li fecycl e of an ASP. Net Web API message. Lifecycle of ASP.net Web API Message Posted by Mudi ta Rathore i n Arti cl es | ASP.NET Programmi ng on May 01, 2013 Tags: ASP.Net, ASP.Net Web API, Li fecycl e of Web API, Web API Tweet 4 0 3878 0 0 Reader Level: 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
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: 01 How to Add Al ertDi al og i n Androi d 02 LINQ To SQL 03 Devel op and Instal l a Wi ndows Servi ce i n C# 04 How to Add Col or Events i n Androi d 05 Excel Servi ces Reports Di spl ayed i n SharePoi nt 2013 06 Database Connecti vi ty i n .NET Framework 07 A si mpl e program to add two numbers i n androi d 08 Fi rst Case of Intent i n Androi d 09 Addi ng CustomToast i n Androi d 10 Addi ng Al ertDi al og Wi th SetSi ngl eChoi ceItems i n Androi d View All TRENDING UP Follow @csharpcorner 5,644 f ollowers H1 Vi sa USA Would you like C# Corner to help you out with USA H1 Visas? Yes No In Focus C# Corner wishes you a Happy Holi Contribute How to Add Al ertDi al og i n Androi d C# Corner Search Share 7 Like Find us on Facebook C# Corner 27,484 people like C# Corner. Facebook social plugin Like POLL RESULT ALL POLLS TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS LINKS NEWS CHAPTERS CAREER ADVICE
3/18/2014 Lifecycle of ASP.net Web API Message http://www.c-sharpcorner.com/UploadFile/2b481f/lifecycle-of-Asp-Net-web-api-message/ 2/4 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. 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 No I don't care Vote 3/18/2014 Lifecycle of ASP.net Web API Message http://www.c-sharpcorner.com/UploadFile/2b481f/lifecycle-of-Asp-Net-web-api-message/ 3/4 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: 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. THIS FEATURE IS SPONSORED BY C# Corner ANNUAL CONFERENCE i s a 3 day annual meeti ng where C# Corner MVPs, authors, chapter l eaders, moderators, edi tors and experts meet, demonstrate, pl an and hang out. One day of the conference i s open to C# Corner members. REGI STER NOW >> ASP.NET WEB API remove a Formatter Intoducti on to Bi ztal k Server 2006 How to Sel f-Host ASP.Net Web API: Part 3 of Many ASP.Net Web API Servi ce i n Wi ndows Phone: Part 4 of Many Web Access Fai l ed Error Create Read Operati on i n Web API Usi ng MVC 4 Getti ng Started Wi th ASP.Net Web API Web.confi g fi l e confi gures fundamental setti ngs Post Data to MVC 4 WEB API .NET 4.5 ASP.Net Web API Servi ce i n Wi ndows Phone: Part 4 of Many RELATED ARTICLES COMMENTS Type your comment here and press Enter Key.... 3/18/2014 Lifecycle of ASP.net Web API Message http://www.c-sharpcorner.com/UploadFile/2b481f/lifecycle-of-Asp-Net-web-api-message/ 4/4 Hosted By CBeyond Cloud Services PRI VACY POLI CY | TERMS & CONDI TI ONS | SI TEMAP | CONTACT US | ABOUT US | REPORT ABUSE 2014 C# Corner. Al l contents are copyri ght of thei r authors. MVPs MOST VIEWED LEGENDS NOW PRIZES AWARDS REVIEWS SURVEY CERTIFICATIONS DOWNLOADS COMMENT USING Facebook social plugin Also post on Facebook Posting as Sangeetha Priya (Not you?) Comment Add a comment... PHOTOS TIPS CONSULTING TRAINING STUDENTS MEMBERS MEDIA KIT SUGGEST AN IDEA