All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
用IHttpModule解决输入中文地址乱码问题(一)_[Asp.Net教程]
数据库连接URL中分号引发的错误_.net资料_编程技术
C#程序中的命名规范
关于XHTML头部声明,什么是DOCTYPE?_[Html教程]
关于.NET中MD5加密与ASP中MD5加密值不同的解决办法_[Asp.Net教程]
Ajax的缺点_[AJAX教程]
PHP读取汉字点阵数据_php资料_编程技术
Asp.NET大文件上传开发总结(一)_[Asp.Net教程]
数据库基础:SQL导出到MySQL_[SQL,Server教程]
Asp.Net,通用数据操作类,(附通用数据基类)_[Asp.Net教程]_0
Reading number is top 10 pictures
看如何给单纯的少女洗脑
哥斯达黎加的门将是如何练成的
Play for Free show breast in a world of ice and snow
Sell the barbecue as says father du breul2
世界各国15岁的MM有什么不同
美丽的少女1
The real super beauty15
The money of more than 100 countries and regions14
美女
ashlynn brooke
Download software ranking
White deer villiage
Tram sex maniac 2 (H) rar bag15
美女游泳记
虚拟机汉化软件
Photoshop 8.0图象编辑软件
网络管理员第三版
WebService在.NET中的实战应用教学视频 → 第3集
电车之狼R
都市狐狸姑娘传
Unix video tutorial19
delv published in(发表于) 2014/1/6 9:10:41 Edit(编辑)
在ASP.NET,Atlas中调用Web,Service—处理错误_[Asp.Net教程]

在ASP.NET,Atlas中调用Web,Service—处理错误_[Asp.Net教程]

在ASP.NET Atlas中调用Web Service—处理错误_[Asp.Net教程]

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

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


public class ComplexWebService : System.Web.Services.WebService {


[WebMethod]
public string BadMethod(int delayTime, bool throwException)
{
// something something
}
}


Atlas产生的JavaScript mash up将会有如下的签名: ComplexWebService.BadMethod(
delayTime,
throwException,
onMethodComplete,
onMethodTimeout,
onMethodError,
onMethodAborted,
userContext,
timeoutInterval,
priority,
useGetMethod,
);
注意到Web Method中的两个参数按照顺序作为了JavaScript方法的前两个参数,接下来还有一些额外的参数:


onMethodComplete:指定当该方法顺利完成并返回时被触发的回调函数名,一般情况下您应该总是指定这个方法。
onMethodTimeout,:指定当该方法执行超时时被触发的函数名。
onMethodError:指定当该方法在执行中遇到异常时被触发的函数名。
onMethodAborted:制定当该方法执行期间被用户取消时被触发的函数名。
userContext:用户上下文对象,在上述四个函数中都可以访问到。
timeoutInterval:设定超时的时间限制,单位毫秒,默认值好像为90000。一般情况下不需要更改。
priority:设定该方法的执行优先级。该优先级将被用于批量AJAX操作(将在下一篇中提到)中。
useGetMethod:是否采用HTTP GET来发送请求,默认为false。
上述这八个属性的顺序必须按照指定的来。但有时候我们只需要指定顺序靠后的某个参数,就不得不同时书写前面的参数。为此,Atlas特意为我们提供了另一种调用方法,将上述八个参数以dictionary的形式传给该方法。例如当我们只需要onMethodComplete和timeoutInterval参数时,可以这样写:


ComplexWebService.BadMethod(
delayTime,
throwException,
{
onMethodComplete: completeHandler,
timeoutInterval: 10000
}
);


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


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


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!";
}
}


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


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







在ASPX页面上添加四个按钮,用来触发下述四种情况:

This is a BAD method, it can:

onclick="return btnWorkFine_onclick()" />
onclick="return btnTimeOut_onclick()" />
onclick="return btnThrowException_onclick()" />
onclick="return btnCanceld_onclick()" />


正常完成,我们指定服务器端没有延时也没有异常,并给出了一个合理的(10秒)的超时时间:


function btnWorkFine_onclick() {
ComplexWebService.BadMethod(
0,
false,
onBadMethodComplete,
onBadMethodTimeout,
onBadMethodError,
onBadMethodAborted,
"btnWorkFine_onclick",
10000
);
}
function onBadMethodComplete(result)
{
alert(result);
}
超时,指定服务器端延时3秒,但超时时间设置成为仅1秒:


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);
}
异常,制定服务器端掷出异常。注意回调函数中可以使用response参数得到详细的错误信息:


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() +
"'\r\nstackTrace = " + result.get_stackTrace();
}

alert(errorString);
}
用户取消,与正常完成类似,不过在发出请求后立刻使用request.abort()取消了操作:


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);
}


作者:Dflying Chen 来源:博客园





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.