Author: Dflying Chen ( http://dflying.cnblogs.com/ )
In the previous post (Calling Web Service in ASP.NET Atlas - Creating Mashup to call remote Web Service (Yahoo! Weather instance)), I introduced the use of BridgeRestProxy to perform Mashup on Web Service. However, in actual development, this simple method is often not enough. We need to write program code to complete some complex logic. That is, use a custom complex Proxy Class instead of the built-in ones in Atlas plus some XML tags in the asbx file. Today we will take a more complex example: Mashup Google's Search Service to learn to use a custom Class to proxy calls to remote Web Services.
First, let’s take a look at the services provided by Google: Google provides us developers with a series of APIs, which you can view at http://api.google.com/ . For the Search API we are going to use today, you can also go to http://api.google.com/Apply for a Google License Key and include this Key in every request to Google. I took a look at Google's documentation, and it said that each License Key only allows 1,000 requests per day. If you need to use Google's Search on a large website, I'm afraid you will have to prepare a bunch of License Keys... Google is really enough Stingy-_-b.
After applying for the License Key, we can get started. Of course, if this is your first time coming into contact with Mashup, you may want to refer to this article of mine: Calling Web Service in ASP.NET Atlas - Creating a Mashup to Call the Remote Web Service (basics and simple examples).
First, use the wsdl.exe tool that comes with Visual Studio to generate the C# code that calls Google Web Service based on its wsdl address:
wsdl.exe http://api.google.com/GoogleSearch.wsdl
will generate GoogleSearchService.cs Add it to the App_Code directory of our Web Site. At this point, we can actually use the classes in this file directly, among which GoogleSearchService.doGoogleSearch() is the method we need. But take a look at this automatically generated messy class. There are many other methods in it, and the doGoogleSearch() method also requires many parameters, so we should wrap this messy file first, encapsulate and simplify its calls.
In this sample program, for each search result, we only need to get its Title, URL and Snippet fields. In order to reduce network traffic, we do not use the search result class that comes with GoogleSearchService.cs, but customize a SearchResultLite Class that only contains the content we need:
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;
}
}
Note that the above SearchResultLite Class must have a default parameterless constructor, and each field must use attributes instead of public members, otherwise Atlas will make errors during the conversion process to JavaScript objects.
Let's wrap GoogleSearchService.doGoogleSearch():
public class GoogleSearchWarpper
{
public SearchResultLite[] Search(string lisenceKey, string query)
{
GoogleSearchService s = new GoogleSearchService();
GoogleSearchResult result = s.doGoogleSearch(
licenseKey,
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();
}
}
In this way, we only need two parameters when calling the Search method, and there is no redundant part in the returned data. Save it as GoogleSearchWarpper.cs.
Next, we need to add the License Key applied for at the beginning to the web.config file, which will be used in subsequent steps:
<appSettings>
<add key="GoogleWebAPILisenceKey" value="!!input your license key here!!"/>
</appSettings>
Let’s look at the declaration of the Bridge file 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>
Notice that the type attribute value of the <proxy> section is specified as the GoogleSearchWarpper class in App_Code, that is, using the Proxy object we just defined. For the two parameters of Search:
the value attribute value of licenseKey is set to % appsettings: GoogleWebAPILisenceKey %. This is a new writing method introduced in the asbx file, which means that its value will be assigned to the appSettings section in the web.config file at runtime. key is the value of GoogleWebAPILisenceKey.
The query will be passed from the client and represents the keyword of the query.
At this point, we can test it in the Atlas page. Of course, the first step is to add ScriptManager to the page and a reference to the above Bridge:
<atlas:ScriptManager ID="scriptManager" runat="server">
<Services>
<atlas:ServiceReference Path="GoogleSearchBridge.asbx" />
</Services>
</atlas:ScriptManager>
Add a piece of HTML to allow users to enter query keywords, trigger queries and display results:
<input id="tbQuery" type="text" />
<input id="btnSearch" type="button" value="Search!" onclick="return btnSearch_onclick()" />
<div id="result">
</div>
Finally, write JavaScript and you can see the use of 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();
}
The sample program can be downloaded here: