About the ASP. NET WEBAPI Message processing pipeline

Source: Internet
Author: User
Tags webhost

After we have developed the ASP. NET WebAPI program, you can deploy the ASP. NET WebAPI by using Webhost homestay or Selfhost boarding method. The so-called webhost homestay is realized through ASP. The so-called Selfhost can be hosted by console, WinForm, WPF, etc. Let's look at the webhost way and the Selfhost way by simple examples.

The demo contains a class library project, a console project, and an empty WebForm project. The Class library project has a class named HomeController that inherits from Apicontroller, and the console project implements the Selfhost;webform project implementation webhost:

Webapi Code:

     Public class Homecontroller:apicontroller    {        publicstring Get (string  fromstr)        {            return $ " The request is from {fromstr} " ;        }    }

Webhost Boarding Method:

Webhost Project's Global file code:

     Public class Global:System.Web.HttpApplication    {        protectedvoid Application_Start (object  sender, EventArgs e)        {             "default""api/{controller}/{fromstr}" );        }    }

Start running webhost project, browser input address http://localhost:34827/api/home/webhost:

Selfhost Boarding Method:

program file code in the Selfhost project:

        Static voidMain (string[] args) {Assembly.Load ("Webapi,version=1.0.0.1,culture=neutral,publickeytoken=null"); Httpselfhostconfiguration Configuration=NewHttpselfhostconfiguration ("http://localhost:34827/"); using(Httpselfhostserver server =Newhttpselfhostserver (configuration)) {Server. Configuration.Routes.MapHttpRoute (Name:"default", Routetemplate:"Api/{controller}/{fromstr}"); Server.                OpenAsync ();            Console.read (); }        }

Start running Selfhost project, browser input address http://localhost:34827/api/home/from_selfhost:

From the above example, you can see that the ASP. Net. Webapi can have multiple boarders, and the reason that ASP. NET Webapi can implement multiple boarding methods is that the ASP. NET Webapi has an abstract pipeline independent of the hosted environment, an abstract pipeline that is an ASP. WEBAPI the message processing pipeline.

The following is a formal entry to today's topic: ASP. NET WEBAPI message processing pipeline. The message processing pipeline for ASP. NET Webapi is an ordered combination of a set of Httpmessagehandler. In this message processing pipeline, in addition to the last Httpmessagehandler, the other is a chain of delegatinghandler of this type, The last Httpmessagehandler is where the final processing of the request (looking for the controller, executing the action). Let's look at the source code to see which types of this ordered combination of message processing pipelines are composed:

After learning about the types that make up the ASP.NETWEBAPI message processing pipeline, let's look at an example to see how it's made. Let's change the previous example by adding two custom Httpmessagehandler and custom httpserver as "Taps" in the WEBAPI project:

     Public classMyhttpmessagehanlder1:delegatinghandler {} Public classMyhttpmessagehanlder2:delegatinghandler {} Public classMyhttpserver:httpserver { PublicMyhttpserver (httpconfiguration configuration):Base(configuration) {} Public New voidInitialize () {Base.        Initialize (); }    }

To modify the HomeController code:

     Public classHomecontroller:apicontroller { Publictuple<ienumerable<string, ienumerable<string>> Get (stringfromstr) {httpconfiguration configuration=Newhttpconfiguration (); Configuration. Messagehandlers.add (NewMyHttpMessageHanlder1 ()); Configuration. Messagehandlers.add (NewMyHttpMessageHanlder1 ()); Myhttpserver Server=Newmyhttpserver (configuration); IEnumerable<string> handler1 =gethandlers (server).            ToArray (); Server.            Initialize (); IEnumerable<string> Handler2 =gethandlers (server).            ToArray (); return Newtuple<ienumerable<string, ienumerable<string>>(Handler1, Handler2); }        Privateienumerable<string>gethandlers (Delegatinghandler handler) {yield returnhandler. GetType ().            Name;  while(Handler. Innerhandler! =NULL)            {                yield returnhandler. Innerhandler.gettype ().                Name; Handler= handler. Innerhandler asDelegatinghandler; if(Handler = =NULL)                     Break; }        }    }

Enter address Http://localhost:34827/api/home/from after running the program:

By the above example, we have verified that Httpserver is the "faucet" of the message processing pipeline, and Httproutingdispatcher is the "dragon tail" of the pipeline. Our custom delegate link is stored in the Messagehanlers property of the Httpconfiguration type, and when Httpserver calls the Initialize method, the delegate chain node in messagehandlers and the "faucet", " Dragon Tail "constitutes a message processing pipeline delegate chain.

With the example above we know the type of the ASP. NET WEBAPI Message processing pipeline and verify the creation of the ASP. NET WEBAPI Message processing pipeline, let's continue to look at what is done inside the "faucet" and "dragon tail" as the message processing pipeline.

When the request flows to the last node of the message processing pipeline, through the source code, as below, we can see that the action completed in the last node is: According to Routedata to find the corresponding controller and action, and then execute action, the resulting response data flow to the "faucet "Httpserver, the Httpserver is returned to the client.

Summarize:

From the previous anatomy, we know that the ASP. NET Webapi has multiple boarding methods because it has a message processing pipeline that is separate from the hosted environment, and we also dissect the various types that make up the ASP. NET WEBAPI Message processing pipeline and discover how the message processing pipeline creates , how the request is circulated in the pipeline and the role of each pipe.

At this point, we have a simple analysis of the ASP. NET WEBAPI message processing pipeline, because personal ability is limited, if not, please correct me.

To elaborate on the ASP. NET WEBAPI Message processing pipeline

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.