2016-06-10 35 views
0

我目前正在使用Windows 10后台任务,并且将用户位置存储在我的数据库中,并使用时间触发器,我试图使用REST Webservice将存储的数据发送到服务器,并且我正在使用RestSharp.Portable API去做。现在,问题在于,当我从后台任务调用Web服务时,请求没有发送到服务器,但是当我在前台执行相同操作时(在我的Windows 10项目中),它的工作正常。有人可以建议我做错了什么吗?时间触发器中的Web服务后台在UWP中不工作的任务?

**我TimeTrigger任务

namespace TimerTask 
{ 
public sealed class TimeTriggerTask : IBackgroundTask 
{ 
    private ApplicationDataContainer userSettings = ApplicationData.Current.LocalSettings; 

    public async void Run(IBackgroundTaskInstance taskInstance) 
    { 
     DatabaseManager dbManager = new DatabaseManager(); 
     var location_records = dbManager.getLocationDetails(); 
     if (MCSManager.Instance.currentClientData == null) 
     { 
      ResourceContext resourceContext = ResourceContext.GetForViewIndependentUse(); 
      ResourceMap resourceMap = MCSExtensions.getResourceMap(); 
      MCSManager.Instance.currentClientData = await new JsonDataHandler().LoadJsonFileforBackgroundTask(resourceMap.GetValue("CLIENT_JSON_FILENAME",resourceContext).ValueAsString, typeof(ClientData)) as ClientData; 
     } 
     if (location_records !=null && location_records.Count>0) 
     { 
      Dictionary<string, object> contentDictionary = new Dictionary<string, object>(); 
      contentDictionary.Add("P_LOC_DATA", location_records); 
      contentDictionary.Add("P_LAST_LOC_LAT",location_records[location_records.Count-1].LATITUDE); 
      contentDictionary.Add("P_LAST_LOC_LNG", location_records[location_records.Count - 1].LONGITUDE); 
      contentDictionary.Add("P_LAST_LOC_UPDATE", location_records[location_records.Count - 1].DATE_TIME); 
      IRestResponse locationTrackingResponse = await new WebServiceUtility().CommonWebservice(new RequestDataGenerator().generateRequestDataForLocationTracking(contentDictionary)); 

      if (locationTrackingResponse.IsSuccess==true && locationTrackingResponse.RawBytes.Length>0) 
      { 
       byte[] decryptedbytes = WebserviceED.finaldecryptedresponse(locationTrackingResponse.RawBytes); 
       string responsejson = Encoding.UTF8.GetString(decryptedbytes, 0, decryptedbytes.Length); 
       JObject userInfo = JObject.Parse(responsejson); 
       string result = (string)userInfo["P_RESULT"]; 
       if(result !=null && result.Equals("1")) 
       { 
        dbManager.TruncateAllLocationTrackingData(); 
        Debug.WriteLine("Data deleted successfully"); 
       } 

      } 
     } 

     // simple example with a Toast, to enable this go to manifest file 
     // and mark App as TastCapable - it won't work without this 
     // The Task will start but there will be no Toast. 
     /*ToastTemplateType toastTemplate = ToastTemplateType.ToastText02; 
     XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate); 
     XmlNodeList textElements = toastXml.GetElementsByTagName("text"); 
     textElements[0].AppendChild(toastXml.CreateTextNode("My first Task - Yeah")); 
     textElements[1].AppendChild(toastXml.CreateTextNode("I'm a message from your background task!")); 
     ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));*/ 
    } 
} 

}

我的web服务调用 公共异步任务CommonWebservice(串encryptedstring) { ResourceContext resourceContext = ResourceContext.GetForViewIndependentUse(); ResourceMap resourceMap = MCSExtensions.getResourceMap();

 var client = new RestClient(BaseUrl + resourceMap.GetValue("WEB_SERVICE_NAME",resourceContext).ValueAsString); 

     RestRequest request = new RestRequest(HttpMethod.Post); 

     byte[] encryptedbytes = System.Text.Encoding.UTF8.GetBytes(encryptedstring); 
     request.AddParameter("", encryptedbytes, ParameterType.RequestBody); 

     var response = await client.Execute(request); 

     return response; 
    } 

我已经在我的应用程序中注册了后台任务。另外,当我们使用异步并等待时,我收到了一个建议延期

+0

请发布您的完整源代码的后台任务。还请确保您在启动应用程序时注册了任务 – thang2410199

+0

@ thang2410199添加了我的后台任务代码,并且我已经注册了它。还请在我的后台任务中建议我应该在哪里添加延期。 –

回答

2

请求未正确执行,后台任务在请求处理之前被终止。

下面的代码添加到您的任务

//get deferral to make the call awaitable BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();

的开始,下面的代码到底

//Complete the task _deferral.Complete();

您可以随时调试你的后台任务,以确保它工作好。