我们是否可以首先从预定的定期任务类调用Web服务,如果是, 我试图在Windows Phone 7.1中使用参数安排定期任务代理类调用webservice方法。虽然我将期望的值传递给webmethod的参数,但在调用方法时我得到一个空引用异常。 我正在从隔离的存储中检索id。
以下是我的代码。
protected override void OnInvoke(ScheduledTask task)
{
if (task is PeriodicTask)
{
string Name = IName;
string Desc = IDesc;
updateinfo(Name, Desc);
}
}
public void updateinfo(string name, string desc)
{
AppSettings tmpSettings = Tr.AppSettings.Load();
id = tmpSettings.myString;
if (name == "" && desc == "")
{
name = "No Data";
desc = "No Data";
}
tservice.UpdateLogAsync(id, name,desc);
tservice.UpdateLogCompleted += new EventHandler<STservice.UpdateLogCompletedEventArgs>(t_UpdateLogCompleted);
}
有人请帮我解决上述问题。
答案 0 :(得分:2)
我之前没有遇到任何问题。您需要确保的一件事是等到异步读取过程完成后才能调用NotifyComplete();
。
以下是我的某个应用中的示例。我不得不删除大部分逻辑,但它应该向您展示流程如何。这使用了稍微修改过的WebClient版本,其中我添加了一个超时,但原则与您正在调用的服务相同...在NotifyComplete()
<结尾之前不要调用t_UpdateLogCompleted
< / p>
以下是示例代码:
private void UpdateTiles(ShellTile appTile)
{
try
{
var wc = new WebClientWithTimeout(new Uri("URI Removed")) { Timeout = TimeSpan.FromSeconds(30) };
wc.DownloadAsyncCompleted += (src, e) =>
{
try
{
//process response
}
catch (Exception ex)
{
// Handle exception
}
finally
{
FinishUp();
}
};
wc.StartReadRequestAsync();
}
private void FinishUp()
{
#if DEBUG
try
{
ScheduledActionService.LaunchForTest(_taskName, TimeSpan.FromSeconds(30));
System.Diagnostics.Debug.WriteLine("relaunching in 30 seconds");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
#endif
NotifyComplete();
}