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

Source: Internet
Author: User
Tags webhost

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

In the previous article, referring to a pattern of pipelines and routes in the Selfhost environment of the ASP. NET Web API framework, this article explains what kind of pipelines and routes are in the ASP. NET Web API framework in the WEBHOST environment.

ASP. NET Web API Routing, Piping
    • ASP. NET Web API Introductory Example

    • ASP. NET Web API Introduction to Routing Objects

    • ASP. NET Web API Piping Model

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

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

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

The following will be the main explanation of the route registration execution process (webhost environment), for the pipeline will not be deliberately described, will be included in the route of the explanation, open to explain the effect is not very good.

Httproute->hostedhttproute->httpwebroute->route

To understand clearly the execution of the route and the pattern of the pipeline, you must be familiar with the routing object, but in the previous "ASP. NET Web API Routing Object", it is only a separate description of the routing object types in each environment, and does not explain the process of transformation.

Now let's explain the “ transformation ” process of the routing object.

Example code 1-1

protected void Application_Start (object sender, EventArgs e) {GlobalConfiguration.Configuration.Routes. Maphttproute ("Defaultapi", "Api/{controller}/{id}", new {controller= "product", id = routeparameter.optional        }); }

Example code 1-1 is a route registration in a webhost environment, according to the Maphttproute () Method we go to define the past should be a httproutecollection type extension method type Httproutecollectionextensions, Since it's the httproutecollectionextensions type of implementation, let's go over and see what's going on.

Example code 1-2

Public static ihttproute maphttproute (this httproutecollection routes, string  name, string routeTemplate, object defaults, object constraints,  Httpmessagehandler handler)         {             if  (routes == null)              {                 throw system.web.http.error.argumentnull ("Routes");             }             httproutevaluedictionary dictionary = new httproutevaluedictionary ( defaults);             httproutevaluedictionary  dictionary2 = new&nbsP Httproutevaluedictionary (constraints);             idictionary<string, object> datatokens = null;             HttpMessageHandler handler2 = handler;             ihttproute route = routes. Createroute (Routetemplate, dictionary, dictionary2, datatokens, handler2);             routes. ADD (Name, route);            return  route;        }

We can see that the return type is Ihttproute, and the build is implemented by an instance of the Httproutecollection type called the Createroute () method, and here are some friends to ask, is this not the way the route registration is implemented in Selfhost? The answer is right, but using polymorphism in webhost to return to other types, then look down.

Now that you've seen what's happening here, that means there's a route object that inherits a type of httproutecollection type and then creates it. Such a rationale is much clearer, in the selfhost environment the httproutecollection type is present in an object of type httpconfiguration and is not used alone. And in the webhost, too.

This time we look back at the definition in the Globalconfiguration type in code 1-1.

Example code 1-3

        private static lazy

From code 1-3 we can see that _configuration static variables using lazy loading, what meaning is the following httpconfiguration type of configuration property if used to instantiate, run off this is not the point.

The point is that in instantiating static variable _configuration It is clear to see that the hostedhttproutecollection type of the route collection type object is used as the constructor parameter. You can take a look at the internal structure of the hostedhttproutecollection on your own.

Now go back to the one that created the route, as shown in code 1-1 and Code 1-2, in the fact that the hostedhttproutecollection type is creating the routing object, which is the usual way to see the implementation code directly.

Example code 1-4

public override Ihttproute Createroute (string uritemplate, idictionary<string, object> defaults, idictionary< String, object> constraints, idictionary<string, object> Datatokens, Httpmessagehandler handler) {Retu    RN New Hostedhttproute (UriTemplate, defaults, Constraints, Datatokens, handler); }

It is clear from code 1-4 that the Hostedhttproute route object is returned, and we can look at the constructor so that we know the process of &ldquo; transform &rdquo;.

        public hostedhttproute (string uriTemplate,  idictionary<string, object> defaults, idictionary<string, object>  Constraints, idictionary<string, object> datatokens, httpmessagehandler handler )     {        routevaluedictionary dictionary  =  (defaults != null)  ? new routevaluedictionary (defaults)  : null ;        routevaluedictionary dictionary2 =  ( Constraints != null)  ? new routevaluedictionary (constraints)  : null;         RouteValueDictionary dictionary3 =  (datatokens ! = null)  ? new routevaluedictionary (datatokens)  : null;         this. OrigiNalroute = new httpwebroute (Uritemplate, dictionary, dictionary2, dictionary3,  httpcontrollerroutehandler.instance, this);         this. handler = handler;   }

In code 1-4, we only need to focus on the assignment of the Originalroute property, which is a property of the Hostedhttproute type and is used to set a reference to the route object. In example code 1-4, an object of type Httpwebroute, the constructor for the Httpwebroute object is not cited here. At this point you can see that an object of type Httpcontrollerroutehandler is the Routehandler (route handler) for the route (Httpwebroute) object.

We all know that ASP. NET Web API framework in the WEBHOST environment is dependent on the ASP, but also through the IHttpModule to carry out the early message interception, the following we look at the code in HttpModule (I think it should be so, if wrong please point.) )

Example code 1-5

    public class WebAPIHttpModule:IHttpModule    {         public void dispose ()          {            throw new  NotImplementedException ();        }         public void init (Httpapplication context)          {            context. postresolverequestcache += context_postresolverequestcache;         }        void context_postresolverequestcache (Object  sender, eventargs e)         {             httpapplication context = sender as httpapplication;             HttpContextWrapper contextWrapper = new  Httpcontextwrapper (context. Context);             routedata routedata  = routetable.routes.getroutedata (Contextwrapper);             requestcontext requestcontext=new requestcontext (ContextWrapper,routeData);             ihttphandler httphandler =  routedata.routehandler.gethttphandler (RequestContext);             IHttpAsyncHandler httpAsyncHandler = httpHandler as  ihttpasynchandler;             Httpasynchandler.beginprocessrequest(Context. Context, null, null);        }      }

In code 1-5 we can see the first is to get the Routedata object instance, to get the Routehandler, and then according to RequestContext get IHttpHandler, and then convert to IHttpAsyncHandler type instance, It then calls its BeginProcessRequest () method to perform the operation.

The above paragraph describes the execution of the above code, some friends may be questioned, how to get routedata?

Here I explain to you, in our code 1-2, there is this code:

Ihttproute route = routes.            Createroute (Routetemplate, dictionary, Dictionary2, Datatokens, Handler2); Routes. ADD (name, route);

First we look at the first sentence, here the route above said is Hostedhttproute object, there is no doubt directly past, and then we look at the second sentence, where the routes is Hostedhttproutecollection object is not false, but this add () The direction added by the method is not hostedhttproutecollection, but what is the current environment, the routetable.routes we said in the Globalconfiguration type at the outset? Asp. NET Framework Environment Yes! There is no doubt that this add () method adds the above-mentioned route (Hostedhttproute object) to the routetable.routes of the current environment, and some friends ask the wrong type. It's wrong. When adding the route (Hostedhttproute object) will be converted to Httpwebroute objects, Httpwebroute objects inherit from the route, you can see the previous space, presumably speaking here you should understand. I'm not going to say much here.

We go back to code 1-5 and get the IHttpHandler instance by Routehandler's Gethttphandler () method after acquiring Routedata. The Routehandler in Routedata is undoubtedly the Httpcontrollerroutehandler type.

Let's take a look at the Gethttphandler () method in the Httpcontrollerroutehandler type:

Protected virtual IHttpHandler Gethttphandler (RequestContext requestcontext) {return new Httpcontrollerhandler (    Requestcontext.routedata); }

You can see that the last action is performed by the Httpcontrollerhandler object type, so let's take a look at this type of definition:

public class Httpcontrollerhandler:ihttpasynchandler, IHttpHandler

Now you understand why you have to turn into IHttpAsyncHandler, because if you call a function that implements the IHttpHandler interface, it will report an exception, Because the IHttpHandler interface is not implemented in the Httpcontrollerhandler type is just an empty shell, then let's look at the static constructor of the Httpcontrollerhandler type:

Figure 1

650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201408/052011458349197.png "width=" 730 "height=" 225 "border=" 0 "hspace=" 0 "vspace=" 0 "title=" "style=" width:730px;height:225px; "/>

This _server is a lazy

Let's take a look at the whole one,

Figure 2

650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201408/052012217099124.png "/>

Finally, the Httpcontrollerdispatcher type is explained in the controller section.

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

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.