时间触发后台任务中的 Web 服务在 UWP 中不起作用?
Webservice in Time Trigger Background Tasks not working in UWP?
我目前正在处理 Windows 10 个后台任务,我正在我的数据库中存储用户位置并使用时间触发器,我正在尝试使用 REST Web 服务将存储的数据发送到服务器,我正在使用RestSharp.Portable API 做到这一点。现在,问题是,当我从后台任务调用 Web 服务时,请求不会发送到服务器,但是当我在前台(在我的 Windows 10 项目中)执行相同操作时,它工作正常。有人可以建议我做错了什么吗?
**我的 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));*/
}
}
}
我的网络服务调用
public 异步任务 CommonWebservice(字符串加密字符串)
{
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;
}
我已经在我的应用程序中注册了后台任务。另外,当我们使用 async 和 await 时,我得到了添加 Deferral 的建议。
请求未正确执行,后台任务在处理请求前被杀死。
将以下代码添加到任务的开头
//get deferral to make the call awaitable
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
和下面的代码到最后
//Complete the task
_deferral.Complete();
您可以随时调试后台任务以确保其正常工作。
我目前正在处理 Windows 10 个后台任务,我正在我的数据库中存储用户位置并使用时间触发器,我正在尝试使用 REST Web 服务将存储的数据发送到服务器,我正在使用RestSharp.Portable API 做到这一点。现在,问题是,当我从后台任务调用 Web 服务时,请求不会发送到服务器,但是当我在前台(在我的 Windows 10 项目中)执行相同操作时,它工作正常。有人可以建议我做错了什么吗?
**我的 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));*/
}
}
}
我的网络服务调用 public 异步任务 CommonWebservice(字符串加密字符串) { 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;
}
我已经在我的应用程序中注册了后台任务。另外,当我们使用 async 和 await 时,我得到了添加 Deferral 的建议。
请求未正确执行,后台任务在处理请求前被杀死。
将以下代码添加到任务的开头
//get deferral to make the call awaitable
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
和下面的代码到最后
//Complete the task
_deferral.Complete();
您可以随时调试后台任务以确保其正常工作。