在ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web Service(Google Search实例)

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

在前一篇贴子(ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web ServiceYahoo!天气实例))中我介绍了使用BridgeRestProxy对Web Service进行Mashup。然而,在实际开发中这种简单的方法往往是不够用的,我们需要书写程序代码来完成一些复杂逻辑。也就是使用自定义的复杂的Proxy Class,而不是Atlas内建的那几种加上一些asbx文件中的XML标记。今天我们来接触一个更复杂的例子:对GoogleSearch Service进行Mashup,以学习使用自定义的Class来代理对远端Web Service的调用。

首先,让我们了解一下Google提供的ServiceGoogle提供给我们开发者一系列的API,您可以到http://api.google.com/查看,对于我们今天要使用的Search API,您还可以到http://api.google.com/googleapi.zip下载它的帮助文档以及示例程序。在开始这个实例之前,我们必须到http://api.google.com/申请一个GoogleLicense Key,并在每一次对Google的请求中包含这个Key。我大概看了一下Google的文档,上面说每个License Key每天只允许1000个请求,这样如果需要在大型的网站上使用GoogleSearch,恐怕要准备一堆的License Key了……Google可真够小气的-_-b

License Key申请好,我们就可以开始了,当然,如果您是第一次接触Mashup,可能还要参考一下我的这篇文章:ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web Service(基础知识以及简单示例)

首先,使用Visual Studio自带的wsdl.exe工具,根据Google Web Servicewsdl地址生成出调用它的C#代码:

wsdl.exe http://api.google.com/GoogleSearch.wsdl

将生成的GoogleSearchService.cs加到我们的Web SiteApp_Code目录中。到这时,我们其实就可以直接使用这个文件中的类了,其中GoogleSearchService.doGoogleSearch()就是我们需要的方法。不过观察一下这个自动生成的乱糟糟的类,其中有好多别的方法,doGoogleSearch()方法也需要好多参数,所以还是先对这个乱糟糟的文件来个包装,封装并简化一下对它的调用。

在这个示例程序中,对于每条搜索结果,我们只要得到它的TitleURL以及Snippet三个字段。为了减少网络流量,我们不使用GoogleSearchService.cs中自带的搜索结果的类,而是自定义一个只包含我们需要内容的SearchResultLite Class

public class SearchResultLite
{
    
private string _title;
    
public string Title
    
{
        
get return _title; }
        
set { _title = value; }
    }


    
private string _url;
    
public string Url
    
{
        
get return _url; }
        
set { _url = value; }
    }


    
private string _snippet;
    
public string Snippet
    
{
        
get return _snippet; }
        
set { _snippet = value; }
    }


    
public SearchResultLite()
    
{
    }


    
public SearchResultLite(string title, string url, string snippet)
    
{
        _title 
= title;
        _url 
= url;
        _snippet 
= snippet;
    }

}

注意上面的SearchResultLite Class中一定要有一个默认的无参的构造函数,并且每一个字段都要使用属性而不是public的成员,否则Atlas在做与JavaScript对象的转换过程中会出错。

下面来对GoogleSearchService.doGoogleSearch()进行包装:

public class GoogleSearchWarpper
{
    
public SearchResultLite[] Search(string lisenceKey, string query)
    
{
        GoogleSearchService s 
= new GoogleSearchService();
        GoogleSearchResult result 
= s.doGoogleSearch(
            lisenceKey,
            query,
            
0,
            
10,
            
false,
            
"",
            
false,
            
"",
            
"",
            
""
        );
        List
<SearchResultLite> resultLites = new List<SearchResultLite>();
        
foreach (ResultElement elem in result.resultElements)
        
{
            SearchResultLite resultLite 
= new SearchResultLite(elem.title, elem.URL, elem.snippet);
            resultLites.Add(resultLite);
        }

        
return resultLites.ToArray();
    }

}

这样我们在调用Search方法的时候只需要两个参数即可,并且返回的数据也没有冗余的部分。将其存为GoogleSearchWarpper.cs

接下来我们要在web.config文件中添加开头申请到的License Key,在后面的步骤中会用到:

<appSettings>
    
<add key="GoogleWebAPILisenceKey" value="!!input your license key here!!"/>
</appSettings>

下面来看Bridge文件GoogleSearchBridge.asbx的声明:

<?xml version="1.0" encoding="utf-8" ?>
<bridge namespace="Dflying" className="GoogleSearch" >
  
<proxy type="GoogleSearchWarpper, App_Code"  />
  
<method name="Search">
    
<input>
      
<parameter name="lisenceKey" value="% appsettings : GoogleWebAPILisenceKey %" serverOnly="true" />
      
<parameter name="query" />
    
</input>
  
</method>
</bridge>

注意到<proxy>段的type属性值被指定为在App_Code中的GoogleSearchWarpper类,也就是使用我们刚刚定义的Proxy对象。对于Search的两个参数:

  1. licenseKeyvalue属性值设置为% appsettings : GoogleWebAPILisenceKey %,这是asbx文件中引入的一个新写法,代表在运行时它的值将被指派为web.config文件中appSettings段中keyGoogleWebAPILisenceKey的值。
  2. query将由客户端传过来,代表查询的关键字。

到此为止,我们可以在Atlas页面中测试一下了,当然第一步还是在页面上添加ScriptManager,还有对上面Bridge的引用:

<atlas:ScriptManager ID="scriptManager" runat="server">
    
<Services>
        
<atlas:ServiceReference Path="GoogleSearchBridge.asbx" />
    
</Services>
</atlas:ScriptManager>

在添加一段HTML,用来让用户输入查询关键字,引发查询并显示结果:

<input id="tbQuery" type="text" />
<input id="btnSearch" type="button" value="Search!" onclick="return btnSearch_onclick()" />
<div id="result">
</div>

最后,编写JavaScript,可以看到其中对Sys.StringBuilder的使用:

function btnSearch_onclick() {
    
var tbQuery = new Sys.UI.TextBox($("tbQuery"));
    Dflying.GoogleSearch.Search(
{'query': tbQuery.get_text()}, onSearchComplete);
}


function onSearchComplete(result) {
    
var sbResult = new Sys.StringBuilder();
    
for (var i = 0; i < result.length; ++i) {
        sbResult.append(
"<hr />");
        sbResult.append(
"<b>" + result[i].Title + "</b><br />");
        sbResult.append(
"<a href=\"" + result[i].Url + "\" target=\"_blank\" >" + result[i].Url + "</a><br />");
        sbResult.append(result[i].Snippet);
    }

    $('result').innerHTML 
= sbResult.toString();
}

恩,浏览器中运行一下,查询一下我吧:

示例程序可以在此下载:https://files.cnblogs.com/dflying/GoogleSearchBridge.zip

注意:想运行这个示例程序,您需要在web.config中的GoogleWebAPILisenceKey部分填入您申请好的License Key

posted on 2006-05-27 15:04  Dflying Chen  阅读(4711)  评论(13编辑  收藏  举报