0
votes

I have app that should backup and restore it's database to SkyDrive. But if I close app on uploading or downloading backup to/from SkyDrive using "start" button (Fast Application Switching) and then return to app using "back" button I catch unhandled NullReferenceException:

void RestoreFromSkyDrive() {
  ShowProgress();

  LiveConnectClient client = new LiveConnectClient(App.Session);
  string id = string.Empty;
  if (client != null) {
    client.GetCompleted += (obj, args) => {
      try {
        List<object> items;

        if (args != null && args.Result != null && args.Result["data"] != null)
          items = args.Result["data"] as List<object>;
        else
          return;

        foreach (object item in items) {
          Dictionary<string, object> file = item as Dictionary<string, object>;
          // fileName is a name of backup-file
          if (file != null && file["name"] != null && file["name"].ToString() == fileName) {
            id = file["id"].ToString();
            if (client != null) client.DownloadCompleted += new EventHandler<LiveDownloadCompletedEventArgs>(client_DownloadCompleted);
            if (client != null) client.DownloadAsync(String.Format("{0}/content", id));
            break;
          }
        }
      }
      catch (Exception ex) {
        MessageBox.Show(ex.Message + " restore");
      }
    };
    if (client != null) {
      client.GetAsync("me/skydrive/files");
    }
  }
}

void client_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e) {
  try {
    if (e.Error == null) {
      Stream stream = e.Result;

      using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) {
        var fileToSave = new IsolatedStorageFileStream(Constants.dbName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, storage);
        stream.CopyTo(fileToSave);
        stream.Flush();
        stream.Close();
        fileToSave.Close();
      }
    }
    else {
      MessageBox.Show(e.Error.Message + " dcompleted mb");
    }
  }
  catch (WebException ex) { MessageBox.Show(ex.Message + " dcompleted ex"); }
  HideProgress();
}

I insert lot of checks for null but anyway I catch NullReferenceException. I don't know what I can do with that.

PS: StackTrace with enabled CLR Exceptions:

System.Net.WebException occurred
Message=WebException
StackTrace:
   at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at System.Delegate.DynamicInvokeOne(Object[] args)
   at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at System.Windows.Threading.Dispatcher.<>c__DisplayClass4.<FastInvoke>b__3()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at System.Delegate.DynamicInvokeOne(Object[] args)
   at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
   at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
   at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
   at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
   at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)

StackTrace with disabled CLR Exceptions:

System.NullReferenceException was unhandled
Message=NullReferenceException
StackTrace:
   at Microsoft.Live.Operations.DownloadOperation.CompleteOperation(Exception error)
   at Microsoft.Live.Operations.DownloadOperation.OnCancel()
   at Microsoft.Live.Operations.WebOperation.OnGetWebResponse(IAsyncResult ar)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)
   at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadPool.WorkItem.doWork(Object o)
   at System.Threading.Timer.ring()
1
I try to use BackgroundDownloadAsync insted of DownloadAsync and at the first blush it's work fine.feeeper

1 Answers

0
votes

Using BackgroundDownload(Upload)Async methods instead of Download(Upload)Async helps me.