国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

在ASP.NET Atlas中調用Web Service—處理錯誤,超時以及響應用戶的取消操作

2019-11-18 17:13:47
字體:
來源:轉載
供稿:網友

作者:Dflying Chen(http://dflying.VEVb.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 = "[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://m.survivalescaperooms.com/Files/dflying/ControlTheWebService.zip


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高邑县| 辽阳市| 罗平县| 湘阴县| 淳化县| 武汉市| 东乌珠穆沁旗| 济源市| 太原市| 禹城市| 安乡县| 武汉市| 通河县| 讷河市| 兴义市| 民权县| 宁城县| 资兴市| 米泉市| 丁青县| 嵊泗县| 云龙县| 鹤岗市| 宜黄县| 平昌县| 新源县| 文安县| 肇东市| 广东省| 玉环县| 安溪县| 唐河县| 西华县| 花垣县| 肃宁县| 开封市| 松滋市| 大新县| 华亭县| 霞浦县| 阿勒泰市|