public static void Initialize() { // create the queue if it doesn't yet exist // this call returns false if the queue was already created if (Queue.CreateIfNotExist()) { TraceLog.TraceInfo(String.Format("Created queue named '{0}'", queueName)); } else { TraceLog.TraceDetail(String.Format("Queue named '{0}' already exists", queueName)); } }
public static MQMessage <T> DequeueMessage <T>() { var msg = Queue.GetMessage(TimeSpan.FromMinutes(1.0d)); if (msg == null) // GetMessage doesn't block for a message { return(null); } TraceLog.TraceDetail(String.Format("Dequeued message ID {0} inserted {1}", msg.Id, msg.InsertionTime.ToString())); byte[] bytes = msg.AsBytes; var ms = new MemoryStream(bytes); DataContractJsonSerializer dcs = new DataContractJsonSerializer(typeof(T)); T content = (T)dcs.ReadObject(ms); MQMessage <T> returnMessage = new MQMessage <T>() { Content = content, MessageRef = msg }; TraceLog.TraceInfo(String.Format("Dequeued a {0}: {1}", content.GetType().Name, content.ToString())); return(returnMessage); }
public override bool ProcessCreate(Item item) { // base method extracts intent if (base.ProcessCreate(item)) { return(true); } // assign category to the grocery item // create a new category fieldvalue in the item to process var categoryFV = item.GetFieldValue(FieldNames.Category, true); // check if the category for this item's name has already been saved under the $User/Grocery list // in this case, store the category in the incoming item var groceryCategoryFV = GetGroceryCategoryFieldValue(item); if (groceryCategoryFV != null && groceryCategoryFV.Value != null) { categoryFV.Value = groceryCategoryFV.Value; return(true); } // get the "intent" which in this case is the normalized name var intentName = item.Name.ToLower(); var intentFV = item.GetFieldValue(ExtendedFieldNames.Intent); if (intentFV != null && intentFV.Value != null) { intentName = intentFV.Value; } // set up the grocery API endpoint GroceryAPI gApi = new GroceryAPI(); gApi.EndpointBaseUri = string.Format("{0}{1}/", HostEnvironment.DataServicesEndpoint, "Grocery"); TraceLog.TraceDetail("GroceryAPI Endpoint: " + gApi.EndpointBaseUri); // try to find the category from the local Grocery Controller try { var results = gApi.Query(GroceryQueries.GroceryCategory, intentName).ToList(); // this should only return one result if (results.Count > 0) { // get the category foreach (var entry in results) { categoryFV.Value = entry[GroceryQueryResult.Category]; if (!String.IsNullOrEmpty(entry[GroceryQueryResult.ImageUrl])) { item.GetFieldValue(FieldNames.Picture, true).Value = entry[GroceryQueryResult.ImageUrl]; } // only grab the first category break; } TraceLog.TraceInfo(String.Format("Grocery API assigned {0} category to item {1}", categoryFV.Value, item.Name)); return(true); } } catch (Exception ex) { TraceLog.TraceException("Grocery API or database commit failed", ex); } // use the Supermarket API to get the grocery category SupermarketAPI smApi = new SupermarketAPI(); #if FALSE // use the synchronous codepath for now // execute the call asynchronously so as to not block the response smApi.BeginQuery(SupermarketQueries.SearchByProductName, item.Name, new AsyncCallback((iar) => { try { var results = smApi.EndQuery(iar); // find the item using a new context var context = Storage.NewUserContext; var groceryItem = context.Items.Single(i => i.ID == item.ID); FieldValue categoryFV = groceryItem.GetFieldValue(FieldNames.Category, true); // get the category foreach (var entry in results) { categoryFV.Value = entry[SupermarketQueryResult.Category]; // only grab the first category TraceLog.TraceInfo(String.Format("ProcessCreate: assigned {0} category to item {1}", categoryFV.Value, item.Name)); return(true); } } catch (Exception ex) { TraceLog.TraceException("ProcessCreate: Supermarket API or database commit failed", ex); } }), null); #else try { var results = smApi.Query(SupermarketQueries.SearchByProductName, intentName); // get the category and image foreach (var entry in results) { categoryFV.Value = entry[SupermarketQueryResult.Category]; if (!String.IsNullOrEmpty(entry[SupermarketQueryResult.Image])) { item.GetFieldValue(FieldNames.Picture, true).Value = entry[SupermarketQueryResult.Image]; } // write the data to the blob store BlobStore.WriteBlobData(BlobStore.GroceryContainerName, entry[SupermarketQueryResult.Name], entry.ToString()); // only grab the first category TraceLog.TraceInfo(String.Format("Supermarket API assigned {0} category to item {1}", categoryFV.Value, item.Name)); return(true); } } catch (Exception ex) { TraceLog.TraceException("Supermarket API or database commit failed", ex); } #endif return(false); }
// Create and add an Intent FieldValue to the item protected void CreateIntentFieldValue(Item item, string intent) { item.GetFieldValue(ExtendedFieldNames.Intent, true).Value = intent; TraceLog.TraceDetail(String.Format("Assigned {0} intent to item {1}", intent, item.Name)); }