using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel;
using BingConnectivity.Microsoft.Bing;
namespace BingConnectivity.BdcModel1
{
public partial class WebResultService
{
public static IEnumerable<string> ReadList(string query)
{
//We can't perform a web search with no query
if (!string.IsNullOrEmpty(query))
{
//Get an instance of the Bing proxy class
BingPortTypeClient client;
SearchRequest request = GetRequestProxy(out client);
//Setup the request parameters
request.Query = query;
//Execute the search
SearchResponse response = client.Search(request);
//Shape the results to suit our requirements
var results = from r in response.Web.Results
select r.Url;
return results;
}
else
{
return null;
}
}
public static string ReadItem(string itemUrl)
{
if (!string.IsNullOrEmpty(itemUrl))
{
BingPortTypeClient client;
SearchRequest request = GetRequestProxy(out client);
request.Query = itemUrl;
SearchResponse response = client.Search(request);
//Since urls are globally unique this query will
//only return one result
return response.Web.Results.Single().Url;
}
else
{
return null;
}
}
private static SearchRequest GetRequestProxy(out
BingPortTypeClient client)
{
//When we added the service reference. Visual Studio automatically
//added configuration information to app.config.
//However since this assembly may be called from a number of processes
//app.config won't be available. As a result we need to manually
//configure the service.
Binding b = new BasicHttpBinding();
EndpointAddress address = new
EndpointAddress("http://api.bing.net:80/soap.asmx");
client = new BingPortTypeClient(b, address);
SearchRequest request = new Microsoft.Bing.SearchRequest();
request.AppId = "ENTER YOUR APPID HERE";
//We're only interested in search the Web source
//See Bing SDK for more details on this parameter
request.Sources = new SourceType[] { SourceType.Web };
return request;
}
}
}