在ASP.NET Atlas中调用Web Service

王朝asp·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

在前一篇文章(在ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web Service(基础知识以及简单示例))中,我介绍了一些Atlas中对远程Web Service进行Mashup的基础知识,并给出了一个最基础的没有丝毫用处例子。今天再回到这个话题上,我将给出一个更复杂点的,但有一些用处的例子——Yahoo! Weather。

废话到此为止,让我们先熟悉一下Yahoo! Weather服务:Yahoo!在其网站上提供了天气预报服务(http://weather.yahoo.com/),并且它也提供了Web Service的接口(http://developer.yahoo.com/weather/)

从上面两个网页上面,我们可以知道Yahoo!提供的天气Service的URL为http://xml.weather.yahoo.com/forecastrss,该服务还有两个参数:

p:要查询天气的地点代码(可以在http://weather.yahoo.com/查询到不同地方的这个代码)。

u:返回结果中温度的单位,f代表华氏度,c代表摄氏度。

看来这个Yahoo! Weather服务还挺简单的,让我们测试下好不好用。先到http://weather.yahoo.com/查出来上海的地点代码为CHXX0116。然后在浏览器中输入http://xml.weather.yahoo.com/forecastrss?p=CHXX0116&u=c,嗯,返回了如下的一段不是很复杂的XML:

Yahoo Weather Service XML Result

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">

<channel>

<title>Yahoo! Weather - Shanghai, CH</title> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Shanghai__CH/*http://xml.weather.yahoo.com/forecast/CHXX0116_c.html</link>

<description>Yahoo! Weather for Shanghai, CH</description>

<language>en-us</language>

<lastBuildDate>Thu, 25 May 2006 11:00 am CST</lastBuildDate>

<ttl>60</ttl>

<yweather:location city="Shanghai" region="" country="CH" />

<yweather:units temperature="C" distance="km" pressure="mb" speed="kph" />

<yweather:wind chill="21" direction="260" speed="14" />

<yweather:atmosphere humidity="78" visibility="299" pressure="0" rising="0" />

<yweather:astronomy sunrise="4:52 am" sunset="6:50 pm" />

<image>

<title>Yahoo! Weather</title>

<width>142</width>

<height>18</height>

<link>http://weather.yahoo.com/</link>

<url>http://us.i1.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>

</image>

<item>

<title>Conditions for Shanghai, CH at 11:00 am CST</title>

<geo:lat>31.17</geo:lat>

<geo:long>121.43</geo:long> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Shanghai__CH/*http://xml.weather.yahoo.com/forecast/CHXX0116_c.html</link>

<pubDate>Thu, 25 May 2006 11:00 am CST</pubDate>

<yweather:condition text="Fog" code="20" temp="21" date="Thu, 25 May 2006 11:00 am CST" />

<description>

<![CDATA[

<img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/20.gif" /><br />

<b>Current Conditions:</b><br />

Fog, 21 C<BR /><BR />

<b>Forecast:</b><BR />

Thu - Scattered Thunderstorms. High: 25 Low: 20<br />

Fri - AM Showers. High: 26 Low: 18<br />

<br />

<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Shanghai__CH/*http://xml.weather.yahoo.com/forecast/CHXX0116_c.html">Full Forecast at Yahoo! Weather</a><BR/>

(provided by The Weather Channel)<br/>

]]>

</description>

<yweather:forecast day="Thu" date="25 May 2006" low="20" high="25" text="Scattered Thunderstorms" code="38" />

<yweather:forecast day="Fri" date="26 May 2006" low="18" high="26" text="AM Showers" code="39" />

<guid isPermaLink="false">CHXX0116_2006_05_25_11_0_CST</guid>

</item>

</channel>

</rss>

<!-- p1.weather.scd.yahoo.com uncompressed/chunked Thu May 25 20:49:07 PDT 2006 -->

我们可以看到,它提供的信息非常全面(连日出日落时间都有……),下面让我们书写asbx Bridge页面来对这个Service进行Mashup。

首先,参考在ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web Service(基础知识以及简单示例)这篇文章中的那个asbx的声明,我们可以写出如下一段:

<?xml version="1.0" encoding="utf-8" ?>

<bridge namespace="Dflying" className="YahooWeatherService">

<proxy type="Microsoft.Web.Services.BridgeRestProxy"

serviceUrl="http://xml.weather.yahoo.com/forecastrss" />

<method name="GetWeather">

<input>

<parameter name="p" />

<parameter name="u" value="c" />

</input>

</method>

</bridge>

其中:<bridge>的namespace和className属性以及<method>的name属性让我们在客户端JavaScript中可以通过Dflying.YahooWeatherService.GetWeather()这样的方法签名来访问这个Mashup。 <proxy>的serviceUrl属性指定了Yahoo! Weather Service的URL。

GetWeather方法中定义了上面列出来的p和u两个参数,其中u参数我们指定了它的默认值为c(代表摄氏度),p参数将由调用者负责传过来。

写到这一步其实也够了,客户端将收到上面浏览器中看到的那一段XML String,并且可以在客户端进行处理并显示。但客户端对XML的处理并不是那么容易,也不是那么高效,同时通过网络传输太多不必要的信息也是一种浪费。所以这里我们利用asbx中内建的Transformer对这段XML处理一下,提取出我们感兴趣的内容并以JSON的形式发给客户端。在<method>段中加入下面一段:

<transforms>

<transform type="Microsoft.Web.Services.XPathBridgeTransformer">

<data>

<attribute name="selector" value="channel" />

<dictionary name="namespaceMapping">

<item name="yweather" value="http://xml.weather.yahoo.com/ns/rss/1.0" />

</dictionary>

<dictionary name="selectedNodes">

<item name="Title" value="title" />

<item name="Description" value="item/description" />

<item name="CurrentCondition" value="item/yweather:condition/@text" />

</dictionary>

</data>

</transform>

</transforms>

其中<transforms>声明表示这个Mashup方法的返回值将会被一些transformer改变一下,里面声明了一个类型为Microsoft.Web.Services.XPathBridgeTransformer的transformer,表示将用XPath表达式来转换。在这个XPathBridgeTransformer中要声明如下部分:

name为selector的一个attribute段,其中指定的value属性为一个XPath表达式,将选取整个XPathBridgeTransformer将用到的数据段。

name为namespaceMapping的一个dictionary段,其中指定了这个XML文件中的namespace映射。如果在下面的选择节点过程中我们用到了某个namespace,那么这里就必须有它的声明。这里我们在其中添加一个对yweather的映射,因为下面要用到。

name为selectedNodes的一个dictionary段,其中每一个item的valu

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航