Author: Dflying Chen ( http://dflying.cnblogs.com/ )
In the previous article of this series (Calling Web Service in ASP.NET Atlas - Introduction and Simple Application), we were familiar with the most basic method of calling Web Service in Atlas, but in actual development, we just make a request and wait. Returning results is not enough. Most of us need to consider handling error timeouts, etc., and also allow users to cancel operations. Fortunately, Atlas' encapsulation of Web Method in Web Service also fully takes these needs into consideration.
Let us give an example of Web Method to illustrate. For example, for the following Web Method:
public class ComplexWebService : System.Web.Services.WebService {
[WebMethod]
public string BadMethod(int delayTime, bool throwException)
{
// something something
}
}
The JavaScript mash up generated by Atlas will have the following signature: ComplexWebService.BadMethod(
delayTime,
throwException,
onMethodComplete,
onMethodTimeout,
onMethodError,
onMethodAborted,
userContext,
timeoutInterval,
priority,
useGetMethod,
);
Notice that the two parameters in the Web Method are used as the first two parameters of the JavaScript method in order, and there are some additional parameters:
onMethodComplete: Specifies the name of the callback function that is triggered when the method completes and returns successfully. In general You should always specify this method.
onMethodTimeout,: Specifies the function name that is triggered when the method execution times out.
onMethodError: Specifies the function name that is triggered when the method encounters an exception during execution.
onMethodAborted: Specify the name of the function that is triggered when the method is canceled by the user during execution.
userContext: User context object, which can be accessed in the above four functions.
timeoutInterval: Set the time limit for timeout in milliseconds. The default value seems to be 90000. Normally no changes are required.
priority: Set the execution priority of this method. This priority will be used in batch AJAX operations (mentioned in the next article).
useGetMethod: Whether to use HTTP GET to send requests, the default is false.
The order of the above eight attributes must be as specified. But sometimes we only need to specify a parameter later in the order, and we have to write the previous parameters at the same time. For this reason, Atlas specially provides us with another calling method, passing the above eight parameters to the method in the form of a dictionary. For example, when we only need onMethodComplete and timeoutInterval parameters, we can write like this:
ComplexWebService.BadMethod(
delayTime,
throwException,
{
onMethodComplete: completeHandler,
timeoutInterval: 10000
}
);
OK, let's take a look at the common processing in the above four callback functions (onMethodComplete, onMethodTimeout, onMethodError and onMethodAborted) under normal circumstances through an example.
First let us complete the Web Service method at the beginning:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = " http://tempuri.org/ ")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ComplexWebService : System.Web.Services.WebService {
[WebMethod]
public string BadMethod(int delayTime, bool throwException)
{
if(throwException)
{
throw new Exception("Sorry, I do not like to do this!");
}
System.Threading.Thread.Sleep(delayTime);
return "Done!";
}
}
You can see that this method has two parameters: delayTime specifies the delay of the method, and throwException specifies whether the method throws an exception. By controlling these two parameters and the timeoutInterval parameter when calling, we can simulate completion, timeout and exception situations.
Then, add ScriptManager to the page and add a reference to this Web Service:
<atlas:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<atlas:ServiceReference Path="ComplexWebService.asmx" />
</Services>
</atlas:ScriptManager>
Add four buttons to the ASPX page to trigger the following four situations: <div>
This is a BAD method, it can:<br />
<input id="btnWorkFine" type="button" value="work fine" onclick="return btnWorkFine_onclick()" />
<input id="btnTimeOut" type="button" value="timeout" onclick="return btnTimeOut_onclick()" />
<input id="btnThrowException" type="button" value="throw an exception" onclick="return btnThrowException_onclick()" />
<input id="btnCanceld" type="button" value="get canceled" onclick="return btnCanceld_onclick()" />
</div>
Completed normally, we specified no delay or exception on the server side, and gave a reasonable (10 seconds) timeout:
function btnWorkFine_onclick() {
ComplexWebService.BadMethod(
0,
false,
onBadMethodComplete,
onBadMethodTimeout,
onBadMethodError,
onBadMethodAborted,
"btnWorkFine_onclick",
10000
);
}
function onBadMethodComplete(result)
{
alert(result);
}
Timeout, specify the server-side delay of 3 seconds, but the timeout is set to only 1 second:
function btnTimeOut_onclick() {
ComplexWebService.BadMethod(
3000,
false,
onBadMethodComplete,
onBadMethodTimeout,
onBadMethodError,
onBadMethodAborted,
"btnTimeOut_onclick",
1000
);
}
function onBadMethodTimeout(request, userContext)
{
var timeoutString = "The call to '" + userContext + "' failed due to time out!";
alert(timeoutString);
}
Exception, specify the exception thrown by the server. Note that you can use the response parameter in the callback function to get detailed error information:
function btnThrowException_onclick() {
ComplexWebService.BadMethod(
0,
true,
onBadMethodComplete,
onBadMethodTimeout,
onBadMethodError,
onBadMethodAborted,
"btnThrowException_onclick",
10000
);
}
function onBadMethodError(result, response, userContext)
{
var errorString = "Test '" + userContext + "' failed!";
if (result == null) {
errorString += " Status code='" + response.get_statusCode() + "'";
}
else {
errorString +=
" Message='" + result.get_message() +
"'rnstackTrace = " + result.get_stackTrace();
}
alert(errorString);
}
User cancellation, similar to normal completion, but using request.abort() to cancel the operation immediately after making the request:
function btnCanceld_onclick() {
var request = ComplexWebService.BadMethod(
2000,
false,
onBadMethodComplete,
onBadMethodTimeout,
onBadMethodError,
onBadMethodAborted,
"btnCanceld_onclick",
10000
);
request.abort();
}
function onBadMethodAborted(request, userContext) {
var errorString = "The call to '" + userContext + "' failed, request is aborted!";
alert(errorString);
}
The sample program can be downloaded here: