首 页 | 精品电影 | 音乐天堂 | 在线游戏 | Flash MTV | 三湘书屋 | 幽默笑话 | 三湘图库 | 美女写真 | IT知识库 | QQ贴图 | 加入书签

网页制作网络编程图形图象操作系统冲浪宝典软件教学网络安全认证考试通信技术电子商务业内动态书籍教程原码

最近更新 文章分类 多媒体类 精品软件

本站搜索:
您的位置:三湘时空 -> IT知识库 -> 文章分类 -> ASP.NET技巧 -> 在ASP.NET Atlas中调用Web Service—处理错误,超时以及响应用户的取消操作
在ASP.NET Atlas中调用Web Service—处理错误,超时以及响应用户的取消操作


文章类别:ASP.NET技巧 来源: 作者: 发表日期:2006-5-18 字体:[ ]

小游戏 | 在线影院 | 幽默笑话 | 源码下载 | Flash MTV | 音乐试听 | 书屋 | 美女写真

作者:Dflying Chen(http://dflying.cnblogs.com/
在本系列的上一篇(在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的引用:

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

 

在ASPX页面上添加四个按钮,用来触发下述四种情况: <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>
正常完成,我们指定服务器端没有延时也没有异常,并给出了一个合理的(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);
}

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

上一篇:内外网同时访问问题_判断访问计算机IP 下一篇:借助工具 为BitComet轻松去广告
本栏目热门文章
·如何实现无刷新的DropdownList联动效果 2005-10-4
·使用HttpWebRequest向网站模拟上传数据 2005-10-4
·当DataSet中包含主/子表时,Update更新步骤 2005-10-6
·ASP.NET2.0实现无刷新客户端回调 2005-11-13
·ASP.NET中文件上传下载方法集合 2006-5-28
·分享个极好的无刷新二级联动下拉列表,同样适用与firefox 2005-10-19
·在Web DataGrid中当鼠标移到某行与离开时行的颜色发生改变( 2005-10-4
·ASP.NET中实现Flash与.NET的紧密集成 2005-11-21
·关于Asp.net页面Page_Load被执行两次的问题 2005-10-4
·ASP.NET极限:页面导航 (翻译) 2005-10-8
新近更新文章
·在VS2003中直接用DREAMWEAVER8打开ASPX文件 2006-6-22
·asp.net2.0中异步调用WebService(异步页) 2006-6-22
·ASP.NET2.0数据库入门之常见错误 2006-6-22
·ASP.NET 2.0中XSLT的使用 2006-6-22
·.Net2.0 使用ConfigurationManager读写配置 2006-6-22
·.net 做的IP 访问限制 2006-6-22
·ASP.Net2.0 GridView 多列排序,显示排序图标,分页 2006-6-22
·数据岛出到Excel最为简易的方法 2006-6-20
·权限树中Checkbox的操作[Asp.Net2.0] 2006-6-20
·使用Data Access Application Block 得到 2006-6-20
首 页 | 软件发布 | 广告联系 | 下载帮助 | 意见反馈 | 网站地图
  CopyRight? 2002-2004 WWW.SXSKY.NET? All Rights Reserved
三湘时空 站长QQ:82675303 Email: