public IAsyncResult BeginQuery(string query, string entity, AsyncCallback callback, object state) { string uri = ConstructQueryUri(query, entity); WebRequest req = WebRequest.Create(uri); SupermarketResult smResult = new SupermarketResult(state); smResult.InnerResult = req.BeginGetResponse( (result) => { if (callback != null) callback(smResult); }, req); return smResult; }
public IAsyncResult BeginQuery(string query, string entity, AsyncCallback callback, object state) { string uri = ConstructQueryUri(query, entity); WebRequest req = WebRequest.Create(uri); SupermarketResult smResult = new SupermarketResult(state); smResult.InnerResult = req.BeginGetResponse( (result) => { if (callback != null) { callback(smResult); } }, req); return(smResult); }
public IEnumerable <SupermarketQueryResult> EndQuery(IAsyncResult result) { if (!result.IsCompleted) { result.AsyncWaitHandle.WaitOne(); } SupermarketResult supermarketResult = result as SupermarketResult; if (supermarketResult == null) { throw new ArgumentException(InvalidAsyncResult, "result"); } WebRequest req = supermarketResult.InnerResult.AsyncState as WebRequest; if (req == null) { throw new ArgumentException(InvalidAsyncResult, "result"); } XElement root; XmlNamespaceManager nsmgr; using (WebResponse resp = req.EndGetResponse(supermarketResult.InnerResult)) using (Stream stream = resp.GetResponseStream()) { XmlReader reader = XmlReader.Create(stream); root = XElement.Load(reader); nsmgr = new XmlNamespaceManager(reader.NameTable); } // Extend the namespace manager with some prefixes needed for processing results. nsmgr.AddNamespace(SupermarketPrefix, SupermarketNS); /* TODO: add error handling * var errors = root.XPathSelectElements(SearchErrorXPath, nsmgr); * if (errors.Count() > 0) * // TODO: Probably want to report these somewhere. * throw new ApplicationException("Search service errors were returned."); */ var searchProductResults = root.XPathSelectElements(SearchProductResultXPath, nsmgr); foreach (var webResult in searchProductResults) { var itemName = webResult.XPathSelectElement(SearchProductResultItemname, nsmgr); var description = webResult.XPathSelectElement(SearchProductResultItemDescription, nsmgr); var category = webResult.XPathSelectElement(SearchProductResultItemCategory, nsmgr); var id = webResult.XPathSelectElement(SearchProductResultItemID, nsmgr); var image = webResult.XPathSelectElement(SearchProductResultItemImage, nsmgr); var aisle = webResult.XPathSelectElement(SearchProductResultAisleNumber, nsmgr); var retval = new SupermarketQueryResult(); retval[SupermarketQueryResult.Name] = itemName != null ? itemName.Value : null; retval[SupermarketQueryResult.Description] = description != null ? description.Value : null; retval[SupermarketQueryResult.Category] = category != null ? category.Value : null; retval[SupermarketQueryResult.ID] = id != null ? id.Value : null; retval[SupermarketQueryResult.Image] = image != null ? image.Value : null; retval[SupermarketQueryResult.Aisle] = aisle != null ? aisle.Value : null; yield return(retval); } }