如果您安装过Atlas的msi文件,并建立了一个Atlas Web Site,那么下面的配置已经为您做好了。否则,启用asbx的支持,您需要如下的操作:
在IIS的虚拟目录下按照下图所示,添加.asbx文件的映射。在web.config的sectionGroup段添加: 在web.config的compilation段添加: 在web.config的httphandlers段添加: 在web.config的httpmodules段添加:
下面让我们通过一个最简单的示例学习Atlas中Mashup的基本概念和asbx文件的基本结构。
首先,编写一个Web Service用来模拟在Internet上存在的一个服务。当然,这个服务没什么实际用处,仅仅是求两个integer的和:
我将这个Web Service置于另外一台主机的IIS上,注意:我们应该更改web.config文件以允许通过HTTP GET对Web Service的访问,而不仅仅是默认的POST: 完成之后,您可以在浏览器中测试一下,访问: http://dflyingchen/MyServices/SimpleWebService.asmx/Add?value1=1&value2=2 (不要忘记更改前面相应的主机名),如果你能得到下图所示的结果,那么恭喜你,Web Service 没问题了。下面就是最有“技术含量”的asbx文件了:
<?
xml version="1.0" encoding="utf-8"
?>
< bridge namespace ="Test" className ="SimpleWebService" >
< proxy type ="Microsoft.Web.Services.BridgeRestProxy"
serviceUrl ="http://dflyingchen/MyServices/SimpleWebService.asmx/Add" />
< method name ="Add" >
< input >
< parameter name ="value1" />
< parameter name ="value2" />
</ input >
</ method >
</ bridge >
< bridge namespace ="Test" className ="SimpleWebService" >
< proxy type ="Microsoft.Web.Services.BridgeRestProxy"
serviceUrl ="http://dflyingchen/MyServices/SimpleWebService.asmx/Add" />
< method name ="Add" >
< input >
< parameter name ="value1" />
< parameter name ="value2" />
</ input >
</ method >
</ bridge >
注意到作为一个合法的XML 文件,asbx 文件第一行是XML 的声明。然后,对于本asbx 文件,有如下部分:
- bridge段,声明该mashup的namespace和class name。注意这两个属性代表的是客户端调用时所见的被mashup过的namespace和class name,并不是远端Web Service的namespace和class name。
- proxy段,声明该mashup的类型,这里我们指定为Microsoft.Web.Services.BridgeRestProxy,相应的,指定一个代表服务URL的string:http://dflyingchen/MyServices/SimpleWebService.asmx/Add 。
- method段,您可以添加多个method段,每一个代表一个定义在上述URL中的服务。这里我们指定了Method的名称,同样的,它代表的仅是客户端调用的名称(这样客户端将以namespace.classname.methodname()的方式调用该mashup)。
- input段,其中通过parameter标记声明了该方法的参数。parameter标记中的name属性代表参数名,注意这里声明的参数名要和远端Web Method的参数名保持一致。这里我们声明了两个参数,与上面定义的Web Method的两个参数对应。
到此为止,一个最简单的asbx文件就完成了。当然,上述介绍与完备相距甚远。关于asbx文件中的其它可选声明,目前并没有官方文档,我只能在接下来的几个例子中根据使用情况适当介绍。
我们再来建立一个Atlas Page测试一下该mashup,首先添加一个ScriptManager,并添加对上述mashup的引用,注意扩展名为asbx,不要写习惯了就直接写成了asmx:
<
atlas:ScriptManager
ID
="sm"
runat
="server"
>
< Services >
< atlas:ServiceReference Path ="SimpleBridge.asbx" />
</ Services >
</ atlas:ScriptManager >
< Services >
< atlas:ServiceReference Path ="SimpleBridge.asbx" />
</ Services >
</ atlas:ScriptManager >
然后添加一段HTML ,用来提供用户输入,引发调用并显示结果;
<
input
id
="input1"
type
="text"
value
="1"
size
="3"
/>
< input id ="input2" type ="text" value ="2" size ="3" />
< input id ="doAdd" type ="button" value ="Add" onclick ="return doAdd_onclick()" />
< div id ="result" />
最后,一小段JavaScript
调用mashup
方法:
< input id ="input2" type ="text" value ="2" size ="3" />
< input id ="doAdd" type ="button" value ="Add" onclick ="return doAdd_onclick()" />
< div id ="result" />
function doAdd_onclick() {
var int1 = $('input1').value;
var int2 = $('input2').value;
Test.SimpleWebService.Add({'value1': int1,'value2': int2}, onAddComplete);
}
function onAddComplete(result) {
$('result').innerHTML = "Result is: " + result;
}
注意调用方法的签名是Test.SimpleWebService.Add()
,其中的每一部分都在asbx
文件中有所声明。另外,返回值是一个XML string
,这里为了简化,就直接输出到页面上了。
var int1 = $('input1').value;
var int2 = $('input2').value;
Test.SimpleWebService.Add({'value1': int1,'value2': int2}, onAddComplete);
}
function onAddComplete(result) {
$('result').innerHTML = "Result is: " + result;
}
浏览器中如下:
示例程序源代码可以在此下载:http://files.cnblogs.com/dflying/SimpleBridgeDemo.rar
在接下来的几个实例中,我将对一些“真实有用”的Web Service进行mashup并应用到我们的程序中。