Understand how XMLHTTP asynchronously calls WebService

Source: Internet
Author: User

Download the source code-24 K

Introduction

Ajax is currently very popular. It is a combination of JavaScript and XML technologies. Asynchronous interaction, JavaScript scripts, and XML Data encapsulation are three main characteristics of Ajax. XMLHTTP is a technology existing in MS a few years ago and has similar architectures in recent years. The previous article introduced WebService calling using scripts on the client. This article will focus on Asynchronous calling.

Asynchronous RPC

Asynchronous Remote Procedure Call (RPC) is an extension of the synchronous RPC mechanism. In synchronous RPC, the thread that sends an RPC call will block the client until the RPC call is completed. Asynchronous RPC allows the calling thread to continue execution and obtain the result later. Similarly, on an asynchronous RPC serverProgramCodeAfter returning to the RPC Runtime (Manager routine), the logical RPC call can continue.

For XMLHTTP WebService calls, you also need to understand the following two points:

HTTP dual-connection restrictions

The HTTP specification indicates that an HTTP client and any server can establish up to two TCP connections at the same time. This prevents a single browser from over-loading the Server due to excessive connection requests when browsing a page (for example, with 120 embedded thumbnails. At this time, the browser will create only two connections, and then start to send 120 HTTP requests through the two pipelines, instead of creating 120 TCP connections and sending HTTP requests through each connection. The problem with this method is that the middle layer may have 50 users requesting connection at the same time. If you have to make a mappoint. NET web service call for each user, 48 users will wait for one of the two pipelines to be idle.

Thread Pool restrictions

ASP. NET processes incoming requests by providing services through a group of threads called the process thread pool. Normally, after a request is passed in, a idle thread in the pool will provide services for it. The problem here is that the process thread pool does not create countless threads to process a large number of requests. It is a good thing to have the maximum number of threads, because if we create threads infinitely, all the resources on the computer can only be used to manage these threads. By limiting the number of threads that can be created, we can keep the system overhead of thread management at a controllable level. If all threads in the thread pool are occupied when a request is passed in, the request will be queued. After the busy thread completes the task, the idle thread can process the new request. This method is actually more effective than switching to a new thread because thread switching between requests is not required. However, if the thread usage efficiency is not high (especially on a very busy Web Server), the waiting Request queue will become very large.

For More thread pool information, refer to here

Consider the Web service calling from the ASP. NET page. If synchronous calling is performed, the running thread is blocked until the Web service call is completed. During the call, the thread cannot perform any other activity. It cannot process other requests and can only wait. If a single processor computer has a default number of worker threads of 20, you only need 20 requests simultaneously to use up all the threads. Future requests must wait in queue.

Important information:Use multi-thread programming technology to call WebService asynchronously.

When asynchronous WebService is called, if multithreading is not used, the problem may occur when two or more threads attempt to access the same data at the same time. The reason is the above two restrictions.

Performance and overhead

Asynchronous calling actually adds additional overhead to request processing, which is 20-30% slower than synchronous calling. Asynchronous calls should not be directly used as a replacement method for Synchronous calls. The purpose of an asynchronous handler is to release the ASP. NET thread pool thread while the handler processes the original request to serve other requests. This function makes sense only when the service request requires a large amount of non-CPU binding time. For example, if the completion of a request depends on the completion of many remote process calls (or web service calls), it is suitable for implementing asynchronous processing programs. If an asynchronous processing program is generated to serve requests that consume a large amount of CPU, only the threads competing with the ASP. NET thread pool will be added, and the overall processing time of the request may be extended. Although asynchronous calling is slow in terms of the time used by a single call, it can significantly increase the overall throughput if it is used effectively (for example, using overlapping calls.

Related Article See

Performance considerations when calling Web services from the ASPX page
CLR Thread Pool
Use threads in server-side Web code and generate asynchronous handlers
Asynchronous web method on the server

Note: This article is the result of the compilation and editing of the above documents. If there is any copyright infringement, please let us know and move it immediately. The source code is also modified. You can obtain the original version from <vs2003 installation path> \ vb7 \ VB samples \ WebServices-winformclient-asyncwebservice \ asynchwebservice

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.