Asp.net|web
For the Atlas program, in some cases, we need to call a large number of web Service in a short time, such as a quick click deletion of a user in a list. At this time network bandwidth, stability and so on tend to cause a longer delay. If these calls can be packaged into a single request, the user simply has to endure a network delay to get the result of processing and indirectly to improve efficiency. It doesn't seem to be a very easy to implement feature, but fortunately, there is a built-in support for a batch call Web service in Atlas, all you need is a simple setup in your program.
The invocation requests for each Web service in Atlas have three priorities: 0: High, 1:, 2: Low, and the default value is medium.
You can specify the priority of this call at the time of the call by priority parameters (refer to: Calling Web service--processing errors in asp.net atlas, timeouts, and responding to cancellation by the user). For high-priority calls, Atlas does not apply a batch call and sends the request immediately; for medium and low priority calls, Atlas will wrap the call within a certain time (refer to the webrequestmanager below) to send it together as a separate request, or send together when the requested request reaches a specified number (refer to the WebRequestManager below). If there are too many requests to be invoked, the request from the middle priority is invoked first.
To enable the bulk call Web service support built in Atlas, you first need to register the handler of the server-side processing batch calls in Web.config (the default Atlas Web Site template has this handler enabled):
<add verb= "*" path= "Atlasbatchcall.axd" type= "Microsoft.Web.Services.MultiRequestHandler" validate= "false"/>
Then add a display declaration to the WebRequestManager in the page's Atlas XML script and set the page to allow batch calls to the 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>
Here you need to be aware of the following three attributes of WebRequestManager:
Enablebatching: Sets whether the page allows batch calls, and the default value is False. Here we should set to true.
BatchSize: Sets the maximum number of requests contained in a batch call, with a default value of 5. When the requested request exceeds this setting, the batch request is issued immediately, even if the set time limit in the Batchdelay is not reached.
Batchdelay: Sets the wait time limit for a batch call. The default value is 1000 (milliseconds). When the wait time limit exceeds this setting, the batch request is issued immediately, even if the number of requests in batchsize is not reached.
Once this is set, a batch call is applied to each Web service request in the page. Therefore, for a single call, you should specify its priority as high.
Let's take a look at an example, first write a Web Service, which has the following Web method, two parameters representing the order of the task (so we can distinguish between the order in which the task is performed) and the priority:
[WebMethod]
public string dotask (int taskID, int priority)
{
if (Priority < 0 | | Priority > 2)
throw new Exception ("Priority can only 0, 1 or 2!");
return string. Format ("Task (ID: {0}, Priority: {1}) finished.", TaskID, Priority);
}
Then follow the first part of the code to enable batch calls in Web.config, and add WebRequestManager to the page, and don't forget that a scriptmanager is needed on the page to refer to the Web Service defined above:
<atlas:scriptmanager id= "ScriptManager" runat= "Server" >
<Services>
<atlas:servicereference path= "Sampleservice.asmx"/>
</Services>
</atlas:ScriptManager>
Add HTML tags. Where the button is used to raise the batch call, and the div is used to display the call result:
<input id= "invoketasks" type= "button" value= "Invoke Task Calls"/>
<div id= "Result"/>
Finally, a JavaScript script that invokes 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}
);
}
}
Function OnComplete (result)
{
$ (' result '). InnerHTML + = result + "<br/>";
}
Note that the Dotask () method accepts two parameters: The Times is used to specify the number of calls, priority is used to specify the priority, and we take advantage of a global variable TaskID to maintain an incremental request order.
In this example, we first call a low-priority request, and then a medium-priority, last two-high-priority. Because high priority does not participate in batch calls, you first see their return:
Since the total number of low priority is 2 and not yet 5, it is sent after a delay of 3000 milliseconds:
You can modify the order of calls in the Invoketasks_onclick () method and the number of calls to analyze how the batch call is implemented.