2012-03-28 57 views
1

我们能否首先从计划周期性任务类中调用webservice,如果是, 我们正尝试使用windows phone中的计划周期性任务代理类中的参数调用webservice方法7.1。虽然我正在将期望值传递给webmethod的参数,但在调用方法时获取空引用异常。 正在从隔离存储中检索ID。在windows phone 7.1中调用计划任务代理类中的web服务

以下是我的代码。

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); 
    } 

有人请帮我解决上述问题。

回答

2

我以前做过这个没有问题。您需要确保的一件事是,在您拨打NotifyComplete();之前,您需要等到异步读取过程完成。

下面是我的一个应用程序示例。我不得不删除大部分逻辑,但它应该告诉你流程如何。这使用稍微修改版本的WebClient,其中我添加了Timeout,但其原理与您要拨打的服务的原理相同...不要拨打NotifyComplete()直到t_UpdateLogCompleted的末尾

下面是示例代码:

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(); 

    } 
+0

解决:其实我忘了初始化我的webservice到构造函数中的新实例。现在,它的工作很好。谢谢你的帮助。 – user1105705 2012-03-29 09:45:43