首页 > 编程 > .NET > 正文

在ASP.NET Atlas中调用Web Service—处理错误

2024-07-10 13:10:45
字体:
来源:转载
供稿:网友
在本系列的上一篇(在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" />
    <input id="btntimeout" type="button" value="timeout" />
    <input id="btnthrowexception" type="button" value="throw an exception" />
    <input id="btncanceld" type="button" value="get canceled" />
</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);
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表