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