【发布时间】:2016-10-11 06:39:27
【问题描述】:
我目前正在处理 Windows 10 后台任务,我正在将用户位置存储在我的数据库中并使用时间触发器,我正在尝试使用 REST Webservice 将存储的数据发送到服务器,我正在使用 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));*/
}
}
}
我的网络服务调用 公共异步任务 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 的建议。
【问题讨论】:
-
请发布您的后台任务的完整源代码。还要确保您在应用启动时注册了任务
-
@thang2410199 添加了我的后台任务代码,我已经注册了。还请建议我应该在我的后台任务中添加延迟。
标签: c# web-services restsharp windows-10-universal