ASP. NET Web API: Host (Hosting)

Source: Internet
Author: User
Tags configuration settings hosting web hosting

Reference page:






The ASP. NET Web API processing architecture describes the three-tier component: Host (hosting), Messaging pipeline (Message Handler pipeline), and controller handling (controllers handling) , this article mainly describes the host (Hosting): Includes the Web Hosting on the ASP. NET Classic pipeline and the self-hosted selfhosting of the WCF stack.

Asp. The web Hosting on the Net classic pipeline

1. ASP. NET routing allows you to use URLs that do not have to be mapped to specific files in your Web site. Because the URL does not have to be mapped to a file, you can use a URL that describes the user's actions and thus is more easily understood by the user, and the route is as important as the ASP. On the ASP. RouteTable static properties Routes add routes to the routing table, for example, the following code is the route defined by the ASP. NET MVC project Template:

protected void Application_Start ()
RegisterRoutes (routetable.routes);

public static void RegisterRoutes (RouteCollection routes)
Routes. Ignoreroute ("{resource}.axd/{*pathinfo}");

Routes. MapRoute (
"Default",//Route name
' {controller}/{action}/{id} ',//URL with parameters
New {controller = "Home", action = "Index", id = urlparameter.optional}

Most of the routing logic is the pipeline event Postresolverequestcache, which is attached to ASP. UrlRoutingModule, for each HTTP request, the module matches the routing rule in the routing set, and if there is a match:

    • Get a route handler from route data
    • Get an HTTP handler that implements the IHttpHandler interface from Route handler
    • Finally, the current request context is mapped to the above HTTP handler

Therefore, the request is sent to this processor at the end of the ASP.

2, integrated Web API, when the host and,web API configuration defined in a single-piece Httpconfiguration object, through the static property globalconfiguration.configuration access. The Web API also defines several routecollection extension methods, called Maphttproute, to define the configuration of the Web API. Take a look at the following example:

httpconfiguration config = globalconfiguration.configuration;
Config. Routes.maphttproute ("Default", "{Controller}/{id}", new {id = urlparameter.optional});
Other configuration settings

The above code mainly accomplishes 2 functions:

    • The static property globalconfiguration.configuration is used to get the configuration that points to the global Routetable.routes routing collection.
    • Add a routing configuration using the Maphttproute extension method.

The route handler that is added to the route collection through the Maphttproute extension method is Httpcontrollerroutehandler, matching the request that was added with the Maphttproute, the associated route Handler returns a new processor type, Httpcontrollerhandler, which implements the asynchronous Iasynchttphandler interface, which is initialized with routing data and contains matching information.

When calling Httpcontrollerhandler's beginprocessrequest method, perform the following action:

    • Create a Httprequestmessage instance from the current HttpContext
    • Create a httpserver using the globalconfiguration.configuration configuration and push the new Httprequestmessage instance into the server pipeline.

After the httpserver gets the request, it enters the host processing new phase (the new pipeline for the Web API). The following figure summarizes the routing process and distribution to the Httpserver instance (Information processing pipeline).

Self-hosted selfhosting based on the WCF stack

We've covered the web hosting on ASP, and we'll cover the WCF stack-based self-hosted selfhosting. Let's start with a self-hosted code:

var config = new Httpselfhostconfiguration ("http://localhost:8080");
Config. Routes.maphttproute ("Default", "{Controller}/{id}",
New {id = routeparameter.optional});
var server = new Httpselfhostserver (config);
Server. OpenAsync (). Wait ();
Console.WriteLine ("Server is opened");

Httpselfhostserver inherits from Httpserver, and uses httpselfhostconfiguration as the configuration class, with the class diagram structure as follows:

The WCF stacks used inside the httpselfhostserver get messages from the transport media and then push them to the top of the message processing pipeline. The following sections briefly describe the high-level architecture of WCF, the characteristics of the Web API self-hosting.

WCF schemas

The WCF architecture is primarily divided into 2 tiers, Channel stacks, and service Model, depending on:

The lower channel stack layer is composed of a bunch of channels and behaves like a classic network protocol stack. Channels are divided into two categories: channels for transmission and protocols. Responsible for transmitting channels by interface and transmission media (e.g. tcp,msmq,http) (yes, I know, HTTP is not just a transport protocol), that is, the reception and delivery of messages. The information channel flow process of the protocol is stacked up and down. A typical use case is to increase the protocol channel digitally signed on the sender and verify the signature of the person on the receiving side. Transmission channels, using encoders to convert byte streams and information transport media bytes instances.

The service model on the upper level executes the message and method calls, and the characters are handled as follows:

    • Converts the received message into a parameter sequence
    • Get to the service instance used
    • Select method to invoke
    • Choosing a method for threading calls

Instead of using the service model layer, however, httpselfhostserver directly consumes messages obtained from the transport channel stack. The Transport channel stack layer is organized using binding, as shown in:

A binding is a set of ordered binding elements, each of which describes a channel or encoder. The first binding element describes the upper channel, and the last binding element describes the underlying channel, in short it is a transmission channel.

Httpselfhostserver and Httpselfhostconfiguration class

The internal Httpselfhostserver.openasync method creates a Httpbinding instance based on the Httpselfhostconfiguration instance property creation and configuration. And then he uses the binding to create a WCF transport stack asynchronously, he also creates a Pump that pushes the message into the stack, transforms it into a httprequestmessage instance, and pushes the new request into Httpserver, which is the process of the message:

With self-hosting, most WCF HTTP binding constraints and settings are available, and httpbinding instances created inside the configuration can be created in 2 ways. The first is to use Httpselfhostconfiguration properties, such as MaxBufferSize and Transfermode, which are used to create httpbinding instances internally. The second method is to create a subclass of Httpselfhostconfiguration and then override the Onconfigurebinding method, which has the opportunity to modify the binding configuration before the channel stack is created.

ASP. WebAPI Hosting Techniques Http://

ASP. NET Web API: Host (Hosting)

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: 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.