在ASP.NET Atlas中调用Web Service——介绍及简单应用

作者:Dflying Chen ( http://dflying.cnblogs.com/

Atlas Framework中包含了对AJAX调用的封装,让您可以很方便的在客户端通过JavaScript调用服务器端方法。在本篇文章中,我将解释一下如何使用Atlas调用服务器端Web Service。

使用Atlas,我们只需要如下步骤即可调用服务器端Web Service:

  1. 在Web Service的方法上加上[WebMethod]属性。
  2. 在ASPX页面上的ScriptManager中添加对这个Web Service的引用。

只需以上两步,Atlas会在运行时为您生成相应的mash up,让您可在客户端JavaScript中通过WebServiceClassName.ServiceMethodName()调用该方法。

让我们先来看一个最简单的例子,调用服务器端Web Service得到两个数的和:

首先建立一个Web Service:SimpleWebService.asmx,并在其中添加一个Service Method,不要忘记标记为[WebMethod]哦:

None.gif [WebMethod]
None.gif
public   int  AddInt( int  int1,  int  int2)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return int1 + int2;
ExpandedBlockEnd.gif}

然后再ASPX页面上的ScriptManager中添加对该Web Service的引用:

None.gif < atlas:ScriptManager  ID ="ScriptManager1"  runat ="server" >
None.gif    
< Services >
None.gif        
< atlas:ServiceReference  Path ="SimpleWebService.asmx"   />
None.gif    
</ Services >
None.gif
</ atlas:ScriptManager >

添加点HTML Code ,让用户输入两个整数:
None.gif Pass simple type to web service - add the two integers: < br  />
None.gif
< input  id ="int1"  type ="text"  value ="1"  size ="3"   /> +
None.gif
< input  id ="int2"  type ="text"  value ="2"  size ="3"   /> =
None.gif
< input  id ="btnAddInt"  type ="button"  value ="?"  onclick ="return btnAddInt_onclick()"   />< br  />
None.gif
< br  />

再书写一点JavaScript ,当用户点击上面的按钮时,调用Web Method 。这里要注意的是JavaScript 中调用Web Method 的格式:前面两个参数int1 ,int2 分别对应着Web Service 声明中的两个参数,后面一个参数onAddIntComplete 表示方法成功返回时的Callback 方法,也就是所谓AJAX 中的A 。同时需要注意的是$() 方法,等同于document.getElementById() 。
ExpandedBlockStart.gif ContractedBlock.gif function  btnAddInt_onclick()  dot.gif {
InBlock.gif    
var int1 = $('int1').value;
InBlock.gif    
var int2 = $('int2').value;
InBlock.gif    SimpleWebService.AddInt(int1, int2, onAddIntComplete);
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
function  onAddIntComplete(result)  dot.gif {
InBlock.gif    $('btnAddInt').value 
= result;
ExpandedBlockEnd.gif}

上面的例子仅仅传递简单类型,然而在现实世界中,我们经常会需要传递一些复杂的类型,让我们看一个传递复杂类型的例子:

本例子同样是一个加法,不过这回操作的类型是复数。让我们先来看看C#中我们的复数的定义(作为示例,这里尽可能的简化)。注意我们应该提供自定义的复杂类型一个无参的构造函数,以便于Atlas自动在C#类型和JavaScript类型中转换:

None.gif public   class  ComplexNumber
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private int real;
InBlock.gif
InBlock.gif    
public int Real
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn real; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ real = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
private int imag;
InBlock.gif
InBlock.gif    
public int Imag
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn imag; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ imag = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public ComplexNumber(int real, int imag)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.real = real;
InBlock.gif        
this.imag = imag;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public ComplexNumber()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

然后是实现复数加法的Web Method,写在同一个Web Service中:

None.gif [WebMethod]
None.gif
public  ComplexNumber AddComplexNumber(ComplexNumber num1, ComplexNumber num2)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return new ComplexNumber(num1.Real + num2.Real, num1.Imag + num2.Imag);
ExpandedBlockEnd.gif}

相应的ASPX 页面中也要添加一些HTML ,让用户输入两个复数:
None.gif Pass complex type to web service - add the two complex numbers: < br  />
None.gif(
< input  id ="cplx1r"  type ="text"  value ="1"  size ="3"   /> +
None.gif
< input  id ="cplx1i"  type ="text"  value ="2"  size ="3"   /> i) + (
None.gif
< input  id ="cplx2r"  type ="text"  value ="3"  size ="3"   /> +
None.gif
< input  id ="cplx2i"  type ="text"  value ="4"  size ="3"   /> i) =  &nbsp;
None.gif
< input  id ="btnAddComplex"  type ="button"  value ="?"  onclick ="return btnAddComplex_onclick()"   />
None.gif
< br  />

然后是相应的JavaScript ,当用户点击上面的按钮时,执行这段JavaScript 以调用Web Method 。
ExpandedBlockStart.gif ContractedBlock.gif function  btnAddComplex_onclick()  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var cplx1 = dot.gif{Real: $('cplx1r').value, Imag: $('cplx1i').value};
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var cplx2 = dot.gif{Real: $('cplx2r').value, Imag: $('cplx2i').value};
InBlock.gif    SimpleWebService.AddComplexNumber(cplx1, cplx2, onAddComplextNumberComplete);
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
function  onAddComplextNumberComplete(result)  dot.gif {
InBlock.gif    $('btnAddComplex').value 
= result.Real.toString() + ' + ' + result.Imag.toString() + 'i';
ExpandedBlockEnd.gif}

浏览器中运行一下, 初始化:
simplews1.JPG

点击第一个问号,调用AddInt () Web Method计算1+2,得到3:
simplews2.JPG

点击第二个问号,调用AddComplexNumber () Web Method计算(1+2i) + (3+4i),得到4+6i:
simplews3.JPG

源代码可以在此下载:http://files.cnblogs.com/dflying/SimpleWebServiceDemo.zip

通过以上两个示例,您已经了解了如何与服务器端Web Service进行复杂通信。但实际开发中,往往还需要进行一些其它的处理,例如对服务器的错误信息的处理,对超时的处理,对用户取消操作的处理等等。Atlas同样提供了对以上需求的内建的支持。在后续文章中,我将全面介绍Atlas对Web Service的mash up中对上述复杂情况的处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值