Author: Dflying Chen ( http://dflying.cnblog.som/ )
For the Atlas program, in some cases, we need to call a large number of Web Services in a short time, such as users in a list quickly clicking to delete. At this time, network bandwidth, stability, etc. will often cause longer delays. If these calls can be packaged into a single request, users only need to endure one network delay to get the processing results, which also indirectly improves efficiency. It seems that this is not an easy function to implement, but fortunately, Atlas has built-in support for batch calling Web Services. All you need is a simple setting in the program.
Each Web Service call request in Atlas has three priorities: 0: high, 1: medium, 2: low, and the default value is medium.
When calling, you can specify the priority of this call through the priority parameter (please refer to: Calling Web Service in ASP.NET Atlas - Handling Errors, Timeouts, and Responding to User Cancellation Operations). For high-priority calls, Atlas does not apply batch calls and will send the request immediately every time; for medium- and low-priority calls, Atlas will wrap the calls within a certain period of time (please refer to the introduction of WebRequestManager below) into one Individual requests are sent together, or when the number of requests to be called reaches a specified number (please refer to the introduction of WebRequestManager below). If there are too many requests to be called, requests with medium priority will be selected to be called first.
To enable Atlas's built-in batch call Web Service support, you first need to register the server-side handler for batch call processing in web.config (the default Atlas Web Site Template has enabled this handler):
<httpHandlers>
<add verb="*" path="atlasbatchcall.axd" type="Microsoft.Web.Services.MultiRequestHandler" validate="false"/>
</httpHandlers>
Then add the display statement of WebRequestManager to the Atlas XML script of the page and set the page to allow batch calls to Web Service: <script type="text/xml-script">
<page xmlns:script=" http://schemas.microsoft.com/xml-script/2005 ">
<components>
<webRequestManager batchSize="5" enableBatching="true" batchDelay="3000" />
</components>
</page>
</script>
What you need to pay attention to here are the following three properties of WebRequestManager:
enableBatching: Set whether the page allows batch calls. The default value is false. Here we should set it to true.
batchSize: Set the maximum number of requests included in a batch call. The default value is 5. When the number of requests to be called exceeds this setting, the batch request will be issued immediately even if the time limit set in batchDelay is not reached.
batchDelay: Set the waiting time limit for a batch call. The default value is 1000 (milliseconds). When the waiting time limit exceeds this setting, the batch request will be issued immediately even if the number of requests in batchSize is not reached.
After this setting, batch calls will be applied to every Web Service request in the page. Therefore, for a single call, you should specify its priority as high.
Let's look at an example. First, write a Web Service, which has the following Web Method. The two parameters represent the order of the task (so that we can distinguish the order of task execution) and the priority:
[WebMethod]
public string DoTask(int taskID, int priority)
{
if (priority < 0 || priority > 2)
throw new Exception("priority can only be 0, 1 or 2!");
return string.Format("Task (ID: {0}, Priority: {1}) finished.", taskID, priority);
}
Then follow the code at the beginning to enable batch calls in web.config, and add WebRequestManager to the page. Don’t forget that a ScriptManager is also needed on the page to reference the Web Service defined above: <atlas:ScriptManager ID="scriptManager" runat=" server">
<Services>
<atlas:ServiceReference Path="SampleService.asmx" />
</Services>
</atlas:ScriptManager>
Add HTML markup. The button is used to trigger batch calls, and the div is used to display the call results:
<input id="invokeTasks" type="button" value="Invoke Task Calls" onclick="return invokeTasks_onclick()" />
<div id="result"/>
Finally, there is the JavaScript script to call the Web Service:
function invokeTasks_onclick()
{
// clear the output
$('result').innerHTML = '';
DoTask(1, 2);
DoTask(1, 1);
DoTask(2, 0);
}
var taskID = 0;
function DoTask(times, priority)
{
for (var i = 0; i < times; ++i)
{
SampleService.DoTask(
taskID++,
priority,
{onMethodComplete: OnComplete, priority: priority }
);
}
}
functionOnComplete(result)
{
$('result').innerHTML += result + "<br />";
}
Notice that the DoTask() method accepts two parameters: times is used to specify the number of calls, priority is used to specify the priority, and we use a global variable taskID to maintain an incrementing request sequence.
In this example, we first invoked a low-priority request, then a medium-priority one, and finally two high-priority ones.
Since high priority does not participate in batch calls, the first thing you see is their return:
Since the total number of medium and low priorities is 2 and has not yet reached 5 , it is sent after a delay of 3000 milliseconds:
You can modify the calling sequence and number of calls in the invokeTasks_onclick() method to analyze how batch calls are implemented.
The sample program can be downloaded here: