【IT168技术文档】
Sys.Net.ServiceMethod -> Sys.Net._WebMethod
在CTP版本中,Sys.Net.ServiceMethod是客户端访问Web Services的基础类,它继承于Sys.Net.WebMethod。同样继承于Sys.Net.WebMethod的还有 Sys.Net.PageMethod,它用于访问PageMethod,这个类在RTM版本中被取消了。现在,客户端用于访问Web Service的类已经变成了Sys.Net._WebMethod(从命名上来看,很明显它不希望用户直接使用这个类),而且在使用上也有了很大的变化。
我们先来回忆一下CTP中Sys.Net.ServiceMethod的使用方式。如下:
而在RTM版本中,Sys.Net._WebMethod的使用方式如下:var serviceMethod = new Sys.Net.ServiceMethod(url, methodName, appUrl); serviceMethod.invoke( parameters, onMethodComplete, onMethodTimeout, onMethodError, onMethodAborted, userContext, timeoutInterval, priority);
CTP版本中 Sys.Net.ServiceMethod中的invoke方法还有一个重载,在RTM版本中就不存在了。在Sys.Net._WebMethod的构造函数中,出现了一个“proxy”参数,这是什么呢?我们来看一下method._execute方法的代码。如下:var method = new Sys.Net._WebMethod(proxy, methodName, fullName, useGet); method._execute(params, onSuccess, onFailure, userContext);
不知道各位朋友看到这段代码的时候感觉如何?真正工作的代码其实是_invokeInternal方法,_execute方法在这里根本没有起什么作用。其实它们两个的关系似乎就相当于CTP版本中Sys.Net.WebMethod类中的invoke和_invoke方法,只是_execute并没有起到一个“调整参数”的作用,因此CTP里的“重载”也就不复存在了。莫非在以后的版本中,_execute方法真的也会提供一个方法重载?function Sys$Net$_WebMethod$_execute(params) { return this._invokeInternal.apply(this, arguments); }
既然真正工作的代码是_invokeInternal,我们就来看一下它的代码。如下:
1 function Sys$Net$_WebMethod$_invokeInternal(params, onSuccess, onFailure, userContext) { 2 /// <param name="params"></param> 3 /// <param name="onSuccess" type="Function" mayBeNull="true" optional="true"></param> 4 /// <param name="onFailure" type="Function" mayBeNull="true" optional="true"></param> 5 /// <param name="userContext" mayBeNull="true" optional="true"></param> 6 var e = Function._validateParams(arguments, [ 7 {name: "params"}, 8 {name: "onSuccess", type: Function, mayBeNull: true, optional: true}, 9 {name: "onFailure", type: Function, mayBeNull: true, optional: true}, 10 {name: "userContext", mayBeNull: true, optional: true} 11 ]); 12 if (e) throw e; 13 14 // 得到fullMethodName,它的作用只是为了“显示”和“提示”之用, 15 // 用户可以自己指定。 16 var methodName = this._fullMethodName; 17 18 // 如果没有指定userContext和回调函数,将从proxy中获得。 19 if (onSuccess === null || typeof onSuccess === 'undefined') 20 onSuccess = this._proxy.get_defaultSucceededCallback(); 21 if (onFailure === null || typeof onFailure === 'undefined') 22 onFailure = this._proxy.get_defaultFailedCallback(); 23 if (userContext === null || typeof userContext === 'undefined') 24 userContext = this._proxy.get_defaultUserContext(); 25 26 // 创建一个新的WebRequest 27 var request = new Sys.Net.WebRequest(); 28 29 // 添加header 30 this.addHeaders(request.get_headers()); 31 // 设置URL 32 request.set_url(this.getUrl(params)); 33 34 if (!params) params = {}; 35 36 // 添加body 37 request.set_body(this.getBody(params)); 38 // 添加onComplete回调函数 39 request.add_completed(onComplete); 40 // 从proxy中获得超时时间并设置 41 var timeout = this._proxy.get_timeout(); 42 if (timeout > 0) request.set_timeout(timeout); 43 // 执行request方法 44 request.invoke(); 45 46 47 function onComplete(response, eventArgs) { 48 if (response.get_responseAvailable()) { 49 var statusCode = response.get_statusCode(); 50 var result = null; 51 52 try { 53 var contentType = response.getResponseHeader("Content-Type"); 54 55 // 根据不同的contentType调用response的不同方法, 56 // 以获得不同的结果 57 if (contentType.startsWith("application/json")) { 58 result = response.get_object(); 59 } 60 else if (contentType.startsWith("text/xml")) { 61 result = response.get_xml(); 62 } 63 else { 64 result = response.get_responseData(); 65 } 66 } 67 catch (ex) {} 68 69 // 如果statusCode表示错误,或者result为WebServiceError对象 70 if (((statusCode < 200) || (statusCode >= 300)) 71 || Sys.Net.WebServiceError.isInstanceOfType(result)) { 72 // 如果用户定义了onFailure回调函数,则使用 73 if (onFailure) { 74 if (!result || !Sys.Net.WebServiceError.isInstanceOfType(result)) { 75 result = new Sys.Net.WebServiceError( 76 false , 77 String.format(Sys.Res.webServiceFailedNoMsg, methodName), 78 "", ""); 79 } 80 result._statusCode = statusCode; 81 onFailure(result, userContext, methodName); 82 } 83 else { // 否则使用alert提示 84 var error; 85 if (result) { 86 error = result.get_exceptionType() + "-- " + result.get_message(); 87 } 88 else { 89 error = response.get_responseData(); 90 } 91 92 alert(String.format(Sys.Res.webServiceFailed, methodName, error)); 93 } 94 } 95 else if (onSuccess) { 96 // 如果定义了onSuccess回调函数,则使用 97 onSuccess(result, userContext, methodName); 98 } 99 } 100 else { 101 var msg; 102 if (response.get_timedOut()) { 103 // 超时了 104 msg = String.format(Sys.Res.webServiceTimedOut, methodName); 105 } 106 else { 107 // 出错 108 msg = String.format(Sys.Res.webServiceFailedNoMsg, methodName) 109 } 110 if (onFailure) { 111 // 如果定义了onFailure回调函数,则使用 112 onFailure( 113 new Sys.Net.WebServiceError(response.get_timedOut(), msg, "", ""), 114 userContext, methodName); 115 } 116 else { 117 alert(msg); 118 } 119 } 120 } 121 122 return request; 123 }