コード例 #1
0
        private async void AppServiceConnection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
        {
            var message = args.Request.Message;

            string command = (string)message["Command"];

            switch (command)
            {
            case "GetSynonym":
            {
                var messageDeferral = args.GetDeferral();

                string term = (string)message["Term"];

                // Call the synonyms service
                SynonymApi api = new SynonymApi(BING_KEY);

                var returnMessage = new ValueSet();

                try
                {
                    var synonyms = await api.GetSynonymsAsync(term);

                    if ((synonyms != null) && (synonyms.Count() > 0))
                    {
                        //Set a result to return to the caller
                        //Serialize the IEnumerable<string> to Json so we can insert into ValueSet
                        returnMessage.Add("Result", JSONHelper.SerializeObject(synonyms));
                    }
                    else
                    {
                        // Report an error back to the caller
                        returnMessage.Add("Error", "No results found");
                    }
                }
                catch (Exception ex)
                {
                    // Error accessing the service
                    // Report an error back to the caller
                    returnMessage.Add("Error", "Synonyms Service not available " + ex.Message + " term=" + term);
                }

                var responseStatus = await args.Request.SendResponseAsync(returnMessage);

                messageDeferral.Complete();
                break;
            }

            case "Quit":
            {
                //Service was asked to quit. Give us service deferral
                //so platform can terminate the background task
                _serviceDeferral.Complete();
                break;
            }
            }
        }
コード例 #2
0
    private async void AppServiceConnection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
    {
      var message = args.Request.Message;

      string command = (string)message["Command"];

      switch (command)
      {
        case "GetSynonym":
          {
            var messageDeferral = args.GetDeferral();

            string term = (string)message["Term"];

            // Call the synonyms service
            SynonymApi api = new SynonymApi(BING_KEY);

            var returnMessage = new ValueSet();

            try
            {
              var synonyms = await api.GetSynonymsAsync(term);

              if ((synonyms != null) && (synonyms.Count() > 0))
              {
                //Set a result to return to the caller
                //Serialize the IEnumerable<string> to Json so we can insert into ValueSet
                returnMessage.Add("Result", JSONHelper.SerializeObject(synonyms));
              }
              else
              {
                // Report an error back to the caller
                returnMessage.Add("Error", "No results found");
              }
            }
            catch (Exception ex)
            {
              // Error accessing the service
              // Report an error back to the caller
              returnMessage.Add("Error", "Synonyms Service not available " + ex.Message + " term=" + term);
            }

            var responseStatus = await args.Request.SendResponseAsync(returnMessage);
            messageDeferral.Complete();
            break;
          }

        case "Quit":
          {
            //Service was asked to quit. Give us service deferral 
            //so platform can terminate the background task 
            _serviceDeferral.Complete();
            break;
          }
      }
    }