在ASP.NET Atlas中调用Web Service——处理错误,超时以及响应用户的取消操作

作者:Dflying Chen ( http://dflying.cnblogs.com/

在本系列的上一篇(在ASP.NET Atlas中调用Web Service——介绍及简单应用)中,我们熟悉了Atlas中调用Web Service的最基础方法,但是在实际开发中,仅仅发出请求并等待返回结果是不够的,我们大都需要考虑对错误超时等的处理,也要允许用户取消操作。幸运的是,Atlas对Web Service中的Web Method的封装也充分考虑到了这些需求。

让我们举一个Web Method的例子来说明,例如,对于如下的Web Method:

ExpandedBlockStart.gif ContractedBlock.gif public   class  ComplexWebService  : System.Web.Services.WebService  dot.gif {
InBlock.gif
InBlock.gif    [WebMethod]
InBlock.gif    
public string BadMethod(int delayTime, bool throwException)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// something something
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}

Atlas 产生的JavaScript mash up 将会有如下的签名:
None.gif ComplexWebService.BadMethod(
None.gif    delayTime, 
None.gif    throwException, 
None.gif    onMethodComplete, 
None.gif    onMethodTimeout, 
None.gif    onMethodError, 
None.gif    onMethodAborted,
None.gif    userContext,
None.gif    timeoutInterval,
None.gif    priority,
None.gif    useGetMethod,
None.gif);

注意到Web Method 中的两个参数按照顺序作为了JavaScript 方法的前两个参数,接下来还有一些额外的参数:
  1. onMethodComplete:指定当该方法顺利完成并返回时被触发的回调函数名,一般情况下您应该总是指定这个方法。
  2. onMethodTimeout,:指定当该方法执行超时时被触发的函数名。
  3. onMethodError:指定当该方法在执行中遇到异常时被触发的函数名。
  4. onMethodAborted:制定当该方法执行期间被用户取消时被触发的函数名。
  5. userContext:用户上下文对象,在上述四个函数中都可以访问到。
  6. timeoutInterval:设定超时的时间限制,单位毫秒,默认值好像为90000。一般情况下不需要更改。
  7. priority:设定该方法的执行优先级。该优先级将被用于批量AJAX操作(将在下一篇中提到)中。
  8. useGetMethod:是否采用HTTP GET来发送请求,默认为false。

上述这八个属性的顺序必须按照指定的来。但有时候我们只需要指定顺序靠后的某个参数,就不得不同时书写前面的参数。为此,Atlas特意为我们提供了另一种调用方法,将上述八个参数以dictionary的形式传给该方法。例如当我们只需要onMethodCompletetimeoutInterval参数时,可以这样写:

None.gif ComplexWebService.BadMethod(
None.gif    delayTime, 
None.gif    throwException, 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        onMethodComplete: completeHandler, 
InBlock.gif        timeoutInterval: 
10000
ExpandedBlockEnd.gif    }

None.gif);

OK,让我们通过一个实例看看在一般情况下上述四种回调函数(onMethodCompleteonMethodTimeoutonMethodErroronMethodAborted)中的常见处理。

首先让我们完成开头部分的Web Service方法:

None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.Web.Services;
None.gif
using  System.Web.Services.Protocols;
None.gif
None.gif[WebService(Namespace 
=   " http://tempuri.org/ " )]
None.gif[WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
ExpandedBlockStart.gifContractedBlock.gif
public   class  ComplexWebService  : System.Web.Services.WebService  dot.gif {
InBlock.gif
InBlock.gif    [WebMethod]
InBlock.gif    
public string BadMethod(int delayTime, bool throwException)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (throwException)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception("Sorry, I do not like to do this!");
ExpandedSubBlockEnd.gif        }

InBlock.gif        System.Threading.Thread.Sleep(delayTime);
InBlock.gif        
return "Done!";
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

可以看到该方法有两个参数:delayTime指定该方法的延时,throwException指定该方法是否掷出异常。通过控制这两个参数以及调用时的timeoutInterval参数,我们就可以模拟完成,超时以及异常的三种情况。

然后,在页面中加入ScriptManager并添加对这个Web Service的引用:

None.gif < atlas:ScriptManager  ID ="ScriptManager1"  runat ="server" >
None.gif    
< Services >
None.gif        
< atlas:ServiceReference  Path ="ComplexWebService.asmx"   />
None.gif    
</ Services >
None.gif
</ atlas:ScriptManager >

在ASPX 页面上添加四个按钮,用来触发下述四种情况:
None.gif < div >
None.gif    This is a BAD method, it can:
< br  />
None.gif    
< input  id ="btnWorkFine"  type ="button"  value ="work fine"  onclick ="return btnWorkFine_onclick()"   />
None.gif    
< input  id ="btnTimeOut"  type ="button"  value ="timeout"  onclick ="return btnTimeOut_onclick()"   />
None.gif    
< input  id ="btnThrowException"  type ="button"  value ="throw an exception"  onclick ="return btnThrowException_onclick()"   />
None.gif    
< input  id ="btnCanceld"  type ="button"  value ="get canceled"  onclick ="return btnCanceld_onclick()"   />
None.gif
</ div >

正常完成,我们指定服务器端没有延时也没有异常,并给出了一个合理的(10 秒)的超时时间:
ExpandedBlockStart.gif ContractedBlock.gif function  btnWorkFine_onclick()  dot.gif {
InBlock.gif    ComplexWebService.BadMethod(
InBlock.gif        
0
InBlock.gif        
false
InBlock.gif        onBadMethodComplete, 
InBlock.gif        onBadMethodTimeout, 
InBlock.gif        onBadMethodError, 
InBlock.gif        onBadMethodAborted,
InBlock.gif        
"btnWorkFine_onclick",
InBlock.gif        
10000
InBlock.gif        );
ExpandedBlockEnd.gif}

None.gif
function  onBadMethodComplete(result) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    alert(result);
ExpandedBlockEnd.gif}

超时,指定服务器端延时3 秒,但超时时间设置成为仅1 秒:
ExpandedBlockStart.gif ContractedBlock.gif function  btnTimeOut_onclick()  dot.gif {
InBlock.gif    ComplexWebService.BadMethod(
InBlock.gif        
3000
InBlock.gif        
false
InBlock.gif        onBadMethodComplete, 
InBlock.gif        onBadMethodTimeout, 
InBlock.gif        onBadMethodError, 
InBlock.gif        onBadMethodAborted,
InBlock.gif        
"btnTimeOut_onclick",
InBlock.gif        
1000
InBlock.gif        );
ExpandedBlockEnd.gif}

None.gif
function  onBadMethodTimeout(request, userContext) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var timeoutString = "The call to '" + userContext + "' failed due to time out!"
InBlock.gif    alert(timeoutString);
ExpandedBlockEnd.gif}

异常,制定服务器端掷出异常。注意回调函数中可以使用response 参数得到详细的错误信息:
ExpandedBlockStart.gif ContractedBlock.gif function  btnThrowException_onclick()  dot.gif {
InBlock.gif    ComplexWebService.BadMethod(
InBlock.gif        
0
InBlock.gif        
true
InBlock.gif        onBadMethodComplete, 
InBlock.gif        onBadMethodTimeout, 
InBlock.gif        onBadMethodError, 
InBlock.gif        onBadMethodAborted,
InBlock.gif        
"btnThrowException_onclick",
InBlock.gif        
10000
InBlock.gif        );
ExpandedBlockEnd.gif}

None.gif
function  onBadMethodError(result, response, userContext) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var errorString = "Test '" + userContext + "' failed!";
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (result == nulldot.gif{
InBlock.gif        errorString 
+= "  Status code='" + response.get_statusCode() + "'";
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
else dot.gif{
InBlock.gif        errorString 
+= 
InBlock.gif             
"  Message='" + result.get_message() +
InBlock.gif            
"'\r\nstackTrace = " + result.get_stackTrace();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    alert(errorString);
ExpandedBlockEnd.gif}

用户取消,与正常完成类似,不过在发出请求后立刻使用request.abort() 取消了操作:
ExpandedBlockStart.gif ContractedBlock.gif function  btnCanceld_onclick()  dot.gif {
InBlock.gif    
var request = ComplexWebService.BadMethod(
InBlock.gif        
2000
InBlock.gif        
false
InBlock.gif        onBadMethodComplete, 
InBlock.gif        onBadMethodTimeout, 
InBlock.gif        onBadMethodError, 
InBlock.gif        onBadMethodAborted,
InBlock.gif        
"btnCanceld_onclick",
InBlock.gif        
10000
InBlock.gif        );
InBlock.gif    request.abort();
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
function  onBadMethodAborted(request, userContext)  dot.gif {
InBlock.gif    
var errorString = "The call to  '" + userContext + "' failed, request is aborted!";
InBlock.gif    alert(errorString);
ExpandedBlockEnd.gif}

截图如下,正常完成:
controlws1.JPG

超时:
controlws2.JPG

异常:
controlws3.JPG

用户取消:
controlws4.JPG

该示例程序可以在此下载:http://files.cnblogs.com/dflying/ControlTheWebService.zip

转载于:https://www.cnblogs.com/dflying/archive/2006/05/17/Atlas_and_Web_Service__Error_Timeout_and_User_Abort_Handling.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值