ASP. NET Web API pipeline model

Source: Internet
Author: User

ASP. NET Web API pipeline model

Objective

The ASP. NET Web API is a stand-alone framework and has its own set of message processing pipelines, whether in the webhost hosting environment or in the Selfhost host environment, where requests and responses are from the message pipeline. This is the way to go, this is a simple introduction to the ASP. NET Web API framework of the pipeline object model.

ASP. NET Web API Routing, Piping

l ASP. NET Web API Introductory Example

l ASP. NET Web API Introduction to Routing Objects

l ASP. NET Web API Piping Model

l ASP. NET Web API selfhost pipelines, routes in the hosting environment

l ASP. NET Web API webhost pipelines, routes in the hosting environment

Pipeline Model Introduction

Httpmessagehandler message handlers (base class)

publicabstractclasshttpmessagehandler:idisposable {Protectedhttpmessagehandler ();        Publicvoiddispose ();        Protectedvirtualvoiddispose (booldisposing); Protectedinternalabstracttask


The above code defines the message handler base class, in which each message processing part of the pipeline is inherited from it.

and defines an asynchronous operation that executes the The SendAsync () method, which is also an entry for each message handler in a series pipeline, is not connected by it.

Delegatinghandler message handlers (base class)

Publicabstractclassdelegatinghandler:httpmessagehandler {Protecteddelegatinghandler ();        Protecteddelegatinghandler (Httpmessagehandlerinnerhandler);         Publichttpmessagehandlerinnerhandler {get; set;}        Protectedoverridevoiddispose (booldisposing); Protectedinternaloverridetask


Here's Delegatinghandler inherit from the Httpmessagehandler type, and Delegatinghandler is also an abstract type, The Delegatinghandler type is not simply inheritance, but rather extends the base class into an object type with a pointing arrow (object reference) , which is the Innerhandler property. The value of the Innerhandler property is the next message handler in the current message handler,the Delegatinghandler type extends to the base class,Httpmessagehandler Type I feel that it exists as a specification, starting from the first handler in the pipeline to the last, except for the last message handler, the other is the subclass of the Delegatinghandler type (and of course Httpmessagehandler ), the last message handler is directly inherited from the Httpmessagehandler type because it is the last handler that does not have to have a property pointing to the next handler. This division of responsibility is really beautiful, it is not good to say that it is beautiful.

httpserver -

publicclasshttpserver : delegatinghandler   {         publichttpserver ();         publichttpserver ( httpconfigurationconfiguration);         publichttpserver ( Httpmessagehandlerdispatcher);         publichttpserver ( Httpconfigurationconfiguration, httpmessagehandlerdispatcher);         publicHttpConfigurationConfiguration { get; }         publicHttpMessageHandlerDispatcher { get; }          protectedoverridevoiddispose (booldisposing);         Protectedvirtualvoidinitialize ();        protectedoverridetask< Httpresponsemessage>sendasync (Httprequestmessagerequest, cancellaTiontokencancellationtoken);} 


The Httpserver type inherits from the Delegatinghandler type, which is handled as the first message in the pipeline, to illustrate the overloaded constructors, if only the default constructor is used . The default for parameters of type httpconfiguration is to instantiate the Httpconfiguration type, while the parameters of the Httpmessagehandler type are instantiated by default The Httproutingdispatcher type of message handler, and is assigned to the Dispatcher property, is the last message handler in the pipeline (the real operation is not actually it, as the rest of the space will be).

Httproutingdispatcher message handlers (Implementation classes - pipe End)

   publicclassHttpRoutingDispatcher : HttpMessageHandler   {         //Fields         privatereadonlyhttpconfiguration_configuration;         privatereadonlyhttpmessageinvoker_defaultinvoker;         // Methods        publichttproutingdispatcher ( httpconfigurationconfiguration);         publichttproutingdispatcher ( Httpconfigurationconfiguration, httpmessagehandlerdefaulthandler);         privatestaticvoidremoveoptionalroutingparameters (idictionary<string, object> RouteValueDictionary);        protectedoverridetask< Httpresponsemessage>sendasync (Httprequestmessagerequest, cancellationtokencancellationtoken);}


The Httproutingdispatcher type inherits from the Httpmessagehandler type, which is said to be the last message handler in the pipeline, so to speak, but it is not actually executed, Instead of generating the Httpcontrollerdispatcher type as the constructor parameter of the Httpmessagehandler type when executing the overloaded constructor, there is no too much elaboration of it. The rest of the space will naturally explain it in detail.

now let's take a look an overview of the ASP. NET Web API pipeline.

Figure 1

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/44/36/wKiom1PgKSfT4RsYAAHCZPgZAb0108.jpg "title=" Webapi1.png "width=" 738 "height=" 203 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:738px;height:203px; "alt=" Wkiom1pgksft4rsyaahczpgzab0108.jpg "/>

(blue lines indicate requests, red lines indicate responses)

This explanation is not too clear below we use the " The ASP. NET Web API starts with an example in the selfhost environment in the example to illustrate, so it's natural for everyone to know about the process.

First we define a message handler type command for Customdelegatinghandler, and inherits from the Delegatinghandler type. The sample code is as follows

Code 1-1

   publicclassCustomDelegatingHandler : DelegatingHandler   {         protectedoverridetask


with that we are The service side of the SELFHOST environment registers the newly created message handler object just after registering the route, the sample code is as follows:

Code 1-2

Staticvoidmain (String[] args)         {             HttpSelfHostConfigurationselfHostConfiguration=                  Newhttpselfhostconfiguration ("Http://localhost/selfhost");             using  (Httpselfhostserverselfhostserver=newhttpselfhostserver ( selfhostconfiguration))             {                  SelfHostServer.Configuration.Routes.MapHttpRoute (                      "Defaultapi",  "Api/{controller}/{id}",  new { id=routeparameter.optional });        &nBsp;        registrationmessagehandler (selfHostServer.Configuration);                   Selfhostserver.openasync ();                  console.writeline ("Server-side service listener is turned on");                 console.read ();             }         }         staticvoidregistrationmessagehandler (httpconfigurationhttpconfiguration)          {             Httpconfiguration. Messagehandlers.add (Newhttpmessagehandlers.customdelegatinghandler ());         }


After the registration is complete, and the server has initiated the open Request listener and the client sends the request, let's look at the client's request and the type, for example.

Figure 2

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/44/36/wKioL1PgKmfShi5LAAOgbQwgN90619.jpg "title=" Webapi2.png "alt=" Wkiol1pgkmfshi5laaogbqwgn90619.jpg "/>

At this point, let's look at the service-side pipeline processing, for example.

Figure 3

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/44/36/wKiom1PgKVuSY0HmAAJFEVKTkr0744.jpg "title=" Webapi3.png "alt=" Wkiom1pgkvusy0hmaajfevktkr0744.jpg "/>

each of the red bezel sections represents a request and response process that corresponds to all the requests in Figure 2 and can be seen from the code 1-1 .

If this example is not obvious, it is not clear to understand the pipeline execution and order, then we define two handlers, and modify the code 1-1, the sample code is as follows:

Code 1-3

   publicclassCustomDelegatingHandler : DelegatingHandler   {         protectedoverridetask


We also add a new message handler where we register for the management handler, as shown in the following example code:

Code 1-4

Staticvoidregistrationmessagehandler (httpconfigurationhttpconfiguration) {httpconfiguration.            Messagehandlers.add (Newhttpmessagehandlers.customdelegatinghandler ()); Httpconfiguration.        Messagehandlers.add (newhttpmessagehandlers.customdelegatinghandler_1 ()); }


this time according to the paragraph before Figure 2, and then look at the service side of the pipeline processing situation, requests or those requests, look at the following:

Figure 4

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/44/36/wKioL1PgKpSjE3ptAARBy2XFCd0591.jpg "title=" Webapi4.png "alt=" Wkiol1pgkpsje3ptaarby2xfcd0591.jpg "/>

(The Red-box representation is the same as above, a request for a response pipeline corresponding to the processing situation)

finally look at a 5 combination of Figure 4, so that better and easier to understand.

Figure 5

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/44/36/wKiom1PgKYmQ05LqAAHeTWgc08Q737.jpg "title=" Webapi14.png "width=" 738 "height=" 216 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:738px;height:216px; "alt=" Wkiom1pgkymq05lqaahetwgc08q737.jpg "/>


This article is from the "Jinyuan" blog, please be sure to keep this source http://jinyuan.blog.51cto.com/8854733/1535778

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.