title: Cortana Background task crashes on mobile but works perfectly on desktop
body: <p>Been looking at this for the past few days now and I'm no closer to a solution. The code below is based on the CortanaVoiceCommand sample at <a href="https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/CortanaVoiceCommand" rel="nofollow noreferrer">https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/CortanaVoiceCommand</a> and the code works perfectly on desktop but the background task is crashing on mobile each time.</p>
  
  <p>In OnTaskCanceled the BackgroundTaskCancellationReason always reports as <strong>SystemPolicy</strong> but I'm no closer to finding a solution.</p>
  
  <p>Appreciate any help anyone can suggest.</p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.ObjectModel;
  using System.Linq;
  using System.Threading.Tasks;
  using Windows.ApplicationModel.AppService;
  using Windows.ApplicationModel.Background;
  using Windows.ApplicationModel.VoiceCommands;
  
  namespace TramTimesVoice
  {
      public sealed class VoiceCommandService : IBackgroundTask
      {
          private BackgroundTaskDeferral backgroundTaskDeferral;
          private IEnumerable services;
          private Service service;
          private Stop stop;
          private VoiceCommandServiceConnection voiceCommandServiceConnection;
  
          public async void Run(IBackgroundTaskInstance taskInstance)
          {
              backgroundTaskDeferral = taskInstance.GetDeferral();
              taskInstance.Canceled += OnTaskCanceled;
  
              AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
  
              if (triggerDetails != null &amp;&amp; triggerDetails.Name == "VoiceCommandService")
              {
                  try
                  {
                      voiceCommandServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                      voiceCommandServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;
  
                      VoiceCommand voiceCommand = await voiceCommandServiceConnection.GetVoiceCommandAsync();
  
                      if (voiceCommand.CommandName == "getServices")
                      {
                          await SendCompletionMessageForServices(voiceCommand.Properties["stop"][0]);
                      }
                      else
                      {
                          LaunchAppInForeground();
                      }
                  }
                  catch (Exception ex)
                  {
                      throw new Exception("Handling Voice Command Failed: " + ex.ToString());
                  }
              }
          }
  
          private async void LaunchAppInForeground()
          {
              VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
              userMessage.SpokenMessage = "Launching Tram Times";
  
              VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage);
              response.AppLaunchArgument = "";
  
              await voiceCommandServiceConnection.RequestAppLaunchAsync(response);
          }
  
          private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
          {
              if (backgroundTaskDeferral != null)
              {
                  backgroundTaskDeferral.Complete();
              }
          }
  
          private void OnVoiceCommandCompleted(VoiceCommandServiceConnection sender, VoiceCommandCompletedEventArgs args)
          {
              if (backgroundTaskDeferral != null)
              {
                  backgroundTaskDeferral.Complete();
              }
          }
  
          private async Task SendCompletionMessageForServices(String input)
          {
              await ShowProgressScreen("Working On It");
  
              IEnumerable stops = new Stops().Get();
  
              foreach (Stop item in stops)
              {
                  if (input == item.Voice)
                  {
                      stop = new Stop()
                      {
                          Code = item.Code,
                          Name = item.Name,
                          Latitude = item.Latitude,
                          Longitude = item.Longitude,
                          ApiCodes = item.ApiCodes,
                          City = item.City,
                          Lines = item.Lines,
                          Voice = item.Voice
                      };
  
                      break;
                  }
              }
  
              if (stop.City == "Blackpool")
              {
                  services = await new Blackpool().Get(stop.ApiCodes);
              }
              else if (stop.City == "Edinburgh")
              {
                  services = await new Edinburgh().Get(stop.ApiCodes);
              }
              else if (stop.City == "London")
              {
                  services = await new London().Get(stop.ApiCodes);
              }
              else if (stop.City == "Manchester")
              {
                  services = await new Manchester().Get(stop.ApiCodes);
              }
              else if (stop.City == "Midland")
              {
                  services = await new Midland().Get(stop.ApiCodes);
              }
              else if (stop.City == "Nottingham")
              {
                  services = await new Nottingham().Get(stop.ApiCodes);
              }
              else if (stop.City == "Sheffield")
              {
                  services = await new Sheffield().Get(stop.ApiCodes);
              }
  
              ObservableCollection&lt;Service&gt; data = new ObservableCollection&lt;Service&gt;();
              foreach (Service item in services)
              {
                  if (item.DestinationScheduled != null)
                  {
                      service = new Service()
                      {
                          Destination = item.Destination,
                          DestinationRealtime = item.DestinationRealtime,
                          DestinationRealtimeFull = item.DestinationRealtimeFull,
                          DestinationScheduled = item.DestinationScheduled,
                          DestinationScheduledFull = item.DestinationScheduledFull
                      };
                      data.Add(service);
                  }
                  else
                  {
                      service = new Service()
                      {
                          Destination = item.Destination,
                          DestinationRealtime = item.DestinationRealtime,
                          DestinationRealtimeFull = item.DestinationRealtimeFull,
                          DestinationScheduled = item.DestinationRealtime,
                          DestinationScheduledFull = item.DestinationRealtimeFull
                      };
                      data.Add(service);
                  }
              }
              data = new ObservableCollection&lt;Service&gt;(data.OrderBy(service =&gt; service.DestinationScheduledFull).ThenBy(service =&gt; service.Destination));
  
              VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
              userMessage.DisplayMessage = "I Found These Services";
              userMessage.SpokenMessage = "I found these services from " + stop.Voice + ". ";
  
              Collection&lt;VoiceCommandContentTile&gt; tiles = new Collection&lt;VoiceCommandContentTile&gt;();
  
              VoiceCommandContentTile tile = new VoiceCommandContentTile();
              tile.AppLaunchArgument = stop.Name;
              tile.ContentTileType = VoiceCommandContentTileType.TitleWithText;
              tile.Title = stop.Name;
  
              if (data.Count &gt; 0)
              {
                  if (data[0].DestinationRealtime != null)
                  {
                      tile.TextLine1 = data[0].DestinationRealtime + " " + data[0].Destination;
                      userMessage.SpokenMessage += data[0].DestinationRealtime + " to " + data[0].Destination + ". ";
                  }
                  else
                  {
                      tile.TextLine1 = data[0].DestinationScheduled + " " + data[0].Destination;
                      userMessage.SpokenMessage += data[0].DestinationScheduled + " to " + data[0].Destination + ". ";
                  }
              }
  
              if (data.Count &gt; 1)
              {
                  if (data[1].DestinationRealtime != null)
                  {
                      tile.TextLine2 = data[1].DestinationRealtime + " " + data[1].Destination;
                      userMessage.SpokenMessage += data[1].DestinationRealtime + " to " + data[1].Destination + ". ";
                  }
                  else
                  {
                      tile.TextLine2 = data[1].DestinationScheduled + " " + data[1].Destination;
                      userMessage.SpokenMessage += data[1].DestinationScheduled + " to " + data[1].Destination + ". ";
                  }
              }
  
              if (data.Count &gt; 2)
              {
                  if (data[2].DestinationRealtime != null)
                  {
                      tile.TextLine3 = data[2].DestinationRealtime + " " + data[2].Destination;
                      userMessage.SpokenMessage += data[2].DestinationRealtime + " to " + data[2].Destination + ". ";
                  }
                  else
                  {
                      tile.TextLine3 = data[2].DestinationScheduled + " " + data[2].Destination;
                      userMessage.SpokenMessage += data[2].DestinationScheduled + " to " + data[2].Destination + ". ";
                  }
              }
  
              tiles.Add(tile);
  
              VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage, tiles);
              response.AppLaunchArgument = "";
  
              await voiceCommandServiceConnection.ReportSuccessAsync(response);
          }
  
          private async Task ShowProgressScreen(String message)
          {
              var userProgressMessage = new VoiceCommandUserMessage();
              userProgressMessage.DisplayMessage = message;
              userProgressMessage.SpokenMessage = message;
  
              VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userProgressMessage);
  
              await voiceCommandServiceConnection.ReportProgressAsync(response);
          }
      }
  }
  </code></pre>