客户端代理使用细节【函数调用/回调函数的完整签名及WebService级别默认属性】

来源:互联网 发布:右下角网络连接红叉 编辑:程序博客网 时间:2024/06/05 05:46

函数调用完整签名

      Invoke(arg1,…,argN,onSucceeded,onFailed,userContext)

      注:Invoke:调用AJAX的JS方法;

       arg1……argN为函数传递的所有参数;

       onSucceeded:为执行成功后的回调函数;

       onFailed:为执行失败后的回调函数;

       userContext:可用用于在执行函数和回调函数间传递参数,为任意类型;

 

 

回调函数完整签名

       onSucceeded(result,userContext,methodName);

       注:onSucceeded:执行成功后的回调函数;

              Result:服务器返回的值;

              userContext:执行函数传入的userContext变量;

              methodName:执行函数的函数名称;

 

       onFailed(error,userContext,methodName);

        注:onFailed:为执行失败调用的回调函数;

        error:错误处理类的一个实例,存储的为出错信息;

        userContext:执行函数传入的userContext变量;

        methodName:执行函数的函数名称;

 

 

WebService级别默认属性

       注:设置此属性后可不必在执行函数的方法体内再次标明

       timeout;//默认超时时间,值小于0表示不限时间

       defaultUserContext;//设置默认的userContext;

       defaultSucceededCallback;//设置默认成功的回调函数;

       defaultFailedCallback;//设置默认失败的回调函数;

-------------------------------------------------------------------------------------------------------------------------------------------

实例:

aspx页面

<asp:ScriptManager ID="ScriptManager1" runat="server"><Services><asp:ServiceReference Path="ErrorHandling.asmx" /></Services></asp:ScriptManager><input type="button" value="getDivision" onclick="getDivision(5, 0)" /><input type="button" value="timeout" onclick="timeout()" /><script language="javascript" type="text/javascript">ErrorHandling.set_defaultFailedCallback(failedCallback);//设置默认处理失败回调函数ErrorHandling.set_timeout(2000);//设置默认超时时间function getDivision(a, b){ErrorHandling.GetDivision(a, b);}function timeout(){ErrorHandling.Timeout();}function failedCallback(error, userContext, methodName){var message = String.format("Timeout: {0}\nMessage: {1}\nExceptionType: {2}\nStackTrace: {3}",error.get_timedOut(),error.get_message(),error.get_exceptionType(),error.get_stackTrace());alert("Error at " + methodName + "\n\n" + message);}</script>

ErrorHandling.asmx页面代码

<%@ WebService Language="C#" Class="ErrorHandling" %>using System;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Web.Script.Services;using System.Threading;[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][ScriptService]public class ErrorHandling  : System.Web.Services.WebService{[WebMethod]public int GetDivision(int a, int b){return a / b;}[WebMethod]public int Timeout(){Thread.Sleep(5000);return 0;}}



原创粉丝点击