您的位置:知识库 » .NET技术

一步一步学Silverlight :综合实例之Live Search

作者: TerryLee  来源: 博客园  发布时间: 2008-10-09 11:27  阅读: 3649 次  推荐: 0   原文链接   [收藏]  

编写ASMX

直接调用API,返回的信息可能有很多,为了简单起见,我们对返回的结果做一些处理,编写一个SearchResultItem类:

public class SearchResultItem
{
    public string Title { get; set; }

    public string Url { get; set; }

    public string Description { get; set; }
}

添加对Live Search API的Service引用,地址为:http://soap.search.live.com/webservices.asmx?wsdl

TerryLee_Silverlight2_0118

在ASMX中对返回的结果进行一些处理,Silverlight程序最后将直接调用ASMX。在调用Live Search时需要指定应用程序ID以及本地化的信息等,查询的参数将在Silverlight程序中调用时传入。

[WebMethod]
public SearchResultItem[] DoSearch(string query)
{
    MSNSearchPortTypeClient s = new MSNSearchPortTypeClient();
    SearchRequest searchRequest = new SearchRequest();
    int arraySize = 1;
    SourceRequest[] sr = new SourceRequest[arraySize];

    sr[0] = new SourceRequest();
    sr[0].Source = SourceType.Web;

    searchRequest.Query = query;
    searchRequest.Requests = sr;

    searchRequest.AppID = "C0680205851CCC0E38946DB8FF74156C1C826A86";
    searchRequest.CultureInfo = "zh-CN";
    SearchResponse searchResponse;

    searchResponse = s.Search(searchRequest);

    List<SearchResultItem> lists = new List<SearchResultItem>();
    foreach (SourceResponse sourceResponse in searchResponse.Responses)
    {
        Result[] sourceResults = sourceResponse.Results;
        foreach (Result sourceResult in sourceResults)
        {
            SearchResultItem item = new SearchResultItem();
            if ((sourceResult.Title != null) && (sourceResult.Title != String.Empty))
                item.Title = sourceResult.Title;

            if ((sourceResult.Description != null) && (sourceResult.Description != String.Empty))
                item.Description = sourceResult.Description;

            if ((sourceResult.Url != null) && (sourceResult.Url != String.Empty))
                item.Url = sourceResult.Url;

            lists.Add(item);
        }
    }
    return lists.ToArray();
}

测试一下我们的服务是否正常:

TerryLee_Silverlight2_0116 

0
0

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻