Introduction to the ASP. NET Web API pipeline model
The ASP is an independent framework and has its own set of message processing pipelines, whether in the webhost hosting environment or in the Selfhost hosting environment requests and responses are from the message pipeline, this is the way to go, this is a simple introduction of the ASP. The pipeline object model in the WEB API framework.
ASP. NET Web API routing, piping
- The ASP. NET Web API begins with an example
- Introduction to the ASP. NET Web API Routing Object
- ASP. NET Web API pipeline model
- ASP. NET Web API selfhost in the hosting environment, pipelines, routes
- ASP. NET Web API webhost in the hosting environment, pipelines, routes
Pipeline Model Introduction
Httpmessagehandler message handlers (base class)
public abstract class httpmessagehandler:idisposable { protected Httpmessagehandler (); public void Dispose (); protected virtual void Dispose (bool disposing); protected internal abstract task SendAsync ( Httprequestmessage request, CancellationToken CancellationToken); }
The above code defines the message handler base class, in which each message processing part of the pipeline is inherited from it.
and defines a SendAsync () method that performs an asynchronous operation, which is also a portal to each message handler in the inline pipeline, but not by its concatenation.
Delegatinghandler message handlers (base class)
Public Abstract classDelegatinghandler:httpmessagehandler {protectedDelegatinghandler (); protectedDelegatinghandler (Httpmessagehandler innerhandler); PublicHttpmessagehandler Innerhandler {Get;Set; } protected Override voidDispose (BOOLdisposing); protected Internal OverrideTaskSendAsync (httprequestmessage request, CancellationToken CancellationToken); }
The delegatinghandler here inherit from the Httpmessagehandler type, and Delegatinghandler is an abstract type, and the Delegatinghandler type is not simply inheritance but extends the base class. , making it 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 others are subclasses of the Delegatinghandler type (and of course the subclasses of Httpmessagehandler), and the last message handler is directly inherited from the Httpmessagehandler type. Because it is the last handler that does not have to point to the next handler of the property, this division of responsibility is really beautiful, it is not good to say that it is beautiful.
Httpserver Message handlers (Implementation class-pipe header)
Public classHttpserver:delegatinghandler { PublicHttpserver (); Publichttpserver (httpconfiguration configuration); PublicHttpserver (Httpmessagehandler dispatcher); PublicHttpserver (httpconfiguration configuration, Httpmessagehandler dispatcher); PublicHttpconfiguration Configuration {Get; } PublicHttpmessagehandler Dispatcher {Get; } protected Override voidDispose (BOOLdisposing); protected Virtual voidInitialize (); protected OverrideTaskSendAsync (httprequestmessage request, CancellationToken CancellationToken); }
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 Httpconfiguration type parameters is to instantiate the httpconfiguration type, whereas the Httpmessagehandler type parameter defaults to the message handler that instantiates the Httproutingdispatcher type. , and is assigned to the Dispatcher property, as the last message handler in the pipeline (the actual operation is not actually it, which is discussed later).
Httproutingdispatcher message handlers (Implementation classes-pipe tails)
Public classHttproutingdispatcher:httpmessagehandler {// Fields Private ReadOnlyhttpconfiguration _configuration; Private ReadOnlyHttpmessageinvoker _defaultinvoker; //Methods PublicHttproutingdispatcher (httpconfiguration configuration); PublicHttproutingdispatcher (httpconfiguration configuration, Httpmessagehandler DefaultHandler); Private Static voidRemoveoptionalroutingparameters (idictionary<string,Object>routevaluedictionary); protected OverrideTaskSendAsync (httprequestmessage request, CancellationToken CancellationToken); }
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, when executing an overloaded constructor, the default generation of the Httpcontrollerdispatcher type is the constructor parameter of the Httpmessagehandler type, and there is no too much elaboration on it, and the rest of the space will naturally explain it in detail.
Let's look at an overview of the ASP. NET Web API pipeline.
Figure 1
(blue lines indicate requests, red lines indicate responses)
The explanation is not too clear. Below we demonstrate by using an example from the selfhost environment in the example "ASP. NET Web API Introduction", so that you will naturally know the process.
First we define a message handler type command for Customdelegatinghandler, and inherit from the Delegatinghandler type. The sample code is as follows
Code 1-1
Public classCustomdelegatinghandler:delegatinghandler {protected OverrideTaskSendAsync (httprequestmessage request, System.Threading.CancellationToken CancellationToken) {Conso Le. WriteLine (Request. Requesturi.originalstring+"____"+request. Method.method); Task<HttpResponseMessage> Responsemessage =Base. SendAsync (Request, CancellationToken); Console.WriteLine (ResponseMessage.Result.RequestMessage.Method.Method); returnResponsemessage; } }
We then register our new message Handler object in the service side of the selfhost environment after registering the route, with the sample code as follows:
Code 1-2
Static voidMain (string[] args) {httpselfhostconfiguration selfhostconfiguration=NewHttpselfhostconfiguration ("Http://localhost/selfhost"); using(Httpselfhostserver selfhostserver =NewHttpselfhostserver (selfhostconfiguration)) {SelfHostServer.Configuration.Routes.MapHttpRoute ("Defaultapi","Api/{controller}/{id}",New{id =routeparameter.optional}); Registrationmessagehandler (selfhostserver.configuration); Selfhostserver.openasync (); Console.WriteLine ("server-side service monitoring turned on"); Console.read (); } } Static voidRegistrationmessagehandler (httpconfiguration httpconfiguration) {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
At this point, let's look at the service-side pipeline processing, for example.
Figure 3
Each of the red bezel sections represents a request and response process that corresponds to all of the requests in Figure 2, and you can see the output from 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 code 1-1, the sample code is as follows:
Code 1-3
Public classCustomdelegatinghandler:delegatinghandler {protected OverrideTaskSendAsync (httprequestmessage request, System.Threading.CancellationToken CancellationToken) {Conso Le. WriteLine ( This. GetType (). Name +":"+ Request. Requesturi.originalstring +"____"+request. Method.method); Task<HttpResponseMessage> Responsemessage =Base. SendAsync (Request, CancellationToken); Console.WriteLine ( This. GetType (). Name +":"+ResponseMessage.Result.RequestMessage.Method.Method); returnResponsemessage; } } Public classCustomdelegatinghandler_1:delegatinghandler {protected OverrideTaskSendAsync (httprequestmessage request, System.Threading.CancellationToken CancellationToken) {Conso Le. WriteLine ( This. GetType (). Name +":"+ Request. Requesturi.originalstring +"____"+request. Method.method); Task<HttpResponseMessage> Responsemessage =Base. SendAsync (Request, CancellationToken); Console.WriteLine ( This. GetType (). Name +":"+ResponseMessage.Result.RequestMessage.Method.Method); returnResponsemessage; }}
We also add a new message handler where we register for the management handler, as shown in the following example code:
Code 1-4
Static void Registrationmessagehandler (httpconfiguration httpconfiguration) { httpconfiguration. Messagehandlers.add (new Httpmessagehandlers.customdelegatinghandler ()); Httpconfiguration. Messagehandlers.add (new httpmessagehandlers.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
(The Red-box representation is the same as above, a request for a response pipeline corresponding to the processing situation)
Finally look at the 15 combined with Figure 4, which is better and easier to understand.
Figure 5
Jinyuan
Source: http://blog.csdn.net/jinyuan0829
This article is copyrighted by the author and Csdn, welcome reprint, but without the consent of the author must retain this statement, and on the article page