客户端访问Web Service方法的一个细节

ZDNet软件频道 时间:2008-07-09 作者:赵劼 | 中国IT实验室 我要评论()
本文关键词:Service web XML 软件
既然ASP.NET AJAX的客户端与服务器端通信完全通过Microsoft AJAX Library的异步通信层进行,那么我们只要得到正在请求WebServicSys.Net.WebRequest对象,调用其abort方法就可以了。

之前遇到一个要求,需要能够取消一个正在进行中的web Service。这也是我第一次遇到这个功能,不过不难,我想。既然ASP.NET AJAX的客户端与服务器端通信完全通过Microsoft AJAX Library的异步通信层进行,那么我们只要得到正在请求web Service的Sys.Net.webRequest对象,调用其abort方法就可以了。但是究竟应该如何得到这个对象呢?于是我粗略地阅读了一下代码。

  首先假设有如下的web Service方法定义(DemoService.asmx):

 

[ScriptService]
public class DemoService  : System.web.Services.webService
{
    [webMethod]
    public string DemoMethod()
    {
        return "Hello World";
    }   
}
  访问DemoService.asmx/jsdebug(或者将其使用ScriptManager引用到页面中之后)就能够得到如下的代理(片断、经过排版)类。

var DemoService = function()
{
    DemoService.initializeBase(this);
    this._timeout = 0;
    this._userContext = null;
    this._succeeded = null;
    this._failed = null;
}
DemoService.prototype =
{
    DemoMethod:function(succeededCallback, failedCallback, userContext)
    {
        return this._invoke(
            DemoService.get_path(),
            'DemoMethod',
            false,
            {},
            succeededCallback,
            failedCallback,
            userContext);
    }
}
DemoService.registerClass('DemoService',Sys.Net.webServiceProxy);
...
  显然,这个代理类继承了Sys.Net.webServiceProxy类,于是我就把目光转向了其中的_invoke方法:

function Sys$Net$webServiceProxy$_invoke(
    ServicePath, methodName, useGet, params, onSuccess, onFailure, userContext) {
    // validation omitted
    ...
    return Sys.Net.webServiceProxy.invoke(
        ServicePath,
        methodName,
        useGet,
        params,
        onSuccess,
        onFailure,
        userContext,
        this.get_timeout());
}
  这下又将操作委托给了Sys.Net.webServiceProxy.invoke静态方法,继续看代码:

Sys.Net.webServiceProxy.invoke = function Sys$Net$webServiceProxy$invoke(
    ServicePath, methodName, useGet, params, onSuccess, onFailure, userContext, timeout) {
    // validation omitted
    ...
    // Create a web request to make the method call
    var request = new Sys.Net.webRequest();
    // preparing request omitted
    ...
    request.invoke();
    function onComplete(response, eventArgs) {
        // method body omitted
    }
    return request;
}
  嗨,这不就是我所需要的Sys.Net.webRequest对象吗?原来想要得到这个对象那么简单,于是我就写下了下面的代码:

var request = DemoService.DemoMethod(onComplete);
  然后在必要时:

request.abort();
  执行,出现了错误:request为undefined,为什么DemoMethod方法调用没有返回request对象?跟踪了代码之后,不大不小地晕了一下,原来问题出在这里:

DemoService._staticInstance = new DemoService();
...
DemoService.DemoMethod = function(onSuccess,onFailed,userContext)
{
    DemoService._staticInstance.DemoMethod(onSuccess,onFailed,userContext);
}
  虽然早就知道web Service代理会在类上创建一个Singleton对象,并且创建静态方法再委托给那个实例上的相应方法,却一直没有意识到这个细节。在上面的静态方法中,居然是直接调用了DemoMethod方法,却没有将结果返回出来,真让我哭笑不得了一下。

  不过问题时非常容易解决的,只要使用如下的方式在客户端调用webService方法就可以了:

var request = DemoService._staticInstance.DemoMethod(onComplete);
  不过这个做法似乎……有些奇怪?那么您也可以这样:

var demoService = new DemoService();
var request = demoService.DemoMethod(onComplete);
  在这里,重新创建一个demoService对象似乎有些多余,不过在某些时候也是非常有用的做法。例如,您需要将操作分为两类,一类的超时时间为5秒,而另一类为10秒,因此您就可以创建两个代理对象,分别设置不同的超时时间。因为超时时间我们只能在Service的级别上设置,而不能在调用方法时指定。

Service

web

XML

软件


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134