asp.net2.0中异步调用WebService(异步页)

由于asp2.0提供了异步页的支持使异步调用WebService的性能有了真正的提升。
使用异步页,首先要设置Async="true",异步页是在Prerender和PrerenderComplete事件间加入Begin,end异步方法实现的,Begin和End方法属于不同的线程。
WS异步页的实现有两种方式:
1、使用等待方法实现异步
通用类,封装了WS
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif        
/// 使用等待方法实现异步
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="name"></param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          private  Account account;
None.gif        
private   string  username;
None.gif        
public  Account Account
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn account; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ account = value; }
ExpandedBlockEnd.gif        }

None.gif        
public   string  Username
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn username; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ username = value; }
ExpandedBlockEnd.gif        }

None.gif        
public  IAsyncResult BeginAsyncGetAccount( object  sender, EventArgs e, AsyncCallback cb,  object  state)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif           
InBlock.gif            
return vb.BeginGetAccountbyName(username,cb,state);
ExpandedBlockEnd.gif        }

None.gif        
public   void  EndAsyncGetAccount(IAsyncResult ar)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            account 
= vb.EndGetAccountbyName(ar);
ExpandedBlockEnd.gif        }

None.gif
ExpandedBlockStart.gifContractedBlock.gif          
/**/ /// <summary>
InBlock.gif        
/// 使用事件驱动的异步
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="username"></param>

None.gif          public   void  GetAccountCompleted(Object source, VB.GetAccountbyNameCompletedEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            account 
= e.Result;
ExpandedBlockEnd.gif        }

None.gif        
public   void  AsGetAccount( string  username)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif                      vb.GetAccountbyNameCompleted 
+= new GetAccountbyNameCompletedEventHandler(GetAccountCompleted);
InBlock.gif            vb.GetAccountbyNameAsync(username)
InBlock.gif            
ExpandedBlockEnd.gif        }
调用方法
None.gif   protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
InBlock.gif        b.Username 
= "dinghao";
InBlock.gif       
InBlock.gif        AddOnPreRenderCompleteAsync(b.BeginAsyncGetAccount, b.EndAsyncGetAccount);
ExpandedBlockEnd.gif    }

None.gif    
protected   void  Page_PreRenderComplete( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
//异步调用结束
InBlock.gif
        VB.Account a = b.Account;
InBlock.gif        AccountIf ai 
= new AccountIf(a);
InBlock.gif        ais[
0= ai;
InBlock.gif        GridView1.DataSource 
= ais;
InBlock.gif        GridView1.DataBind();
ExpandedBlockEnd.gif    }
由于AddOnPreRenderCompleteAsync的两个委托都是Void类型,所以在通用类中要加入有返回值的属性如:Account供主调方法使用,另外委托中没有异步方法的参数信息,要加入参数的属性如:Username
2、事件驱动的异步(2.0新增)
调用方法:
None.gif protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
InBlock.gif        b.AsGetAccount(
"dinghao");
InBlock.gif     
ExpandedBlockEnd.gif    }

None.gif    
protected   void  Page_PreRenderComplete( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
//异步调用结束
InBlock.gif
        VB.Account a = b.Account;
InBlock.gif        AccountIf ai 
= new AccountIf(a);
InBlock.gif        ais[
0= ai;
InBlock.gif        GridView1.DataSource 
= ais;
InBlock.gif        GridView1.DataBind();
ExpandedBlockEnd.gif    }
这种调用方式,用的是*Completed事件,在*Async完成时触发,这种调用方式可以
省去Account,Username属性,用起来比较简单
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值