Search code examples
c#windows-phone-7async-ctp

Async CTP on Windows Phone - cannot using offline?


I have this WP7 application for reading web feeds. I'm using Async CTP for downloading feeds, save them locally and use them, so I would be able to use App offline.

Everything works great when I'm connected to wifi or cellular. But when I'm offline, Application crash on starting with error:

The remote server returned an error: NotFound.

Call stack location:

AsyncCTPLibrary_Phone.dll!System.Runtime.CompilerServices.AsyncVoidMethodBulder.SetException.AnonymousMethod_2(object state)

StackTrace:

at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result) at System.Net.Browser.ClientHttpWebRequest.<>c_DisplayClassa.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()

Any idea why? I'm not automatically downlodading anything...

Edit: Here's starting part of code, I realy try to avoid calling web client if I'm offline:

private void MainPage_Loaded(object sender, RoutedEventArgs e)
 {
     MainPage_Init();
 }


private async void MainPage_Init()
 {
     string isConfig = await Load_Config("");

         if (isLoaded == false)
         {
        if (RSS.Parameters.AutoUpdate == "On")
                {
                    string isDataOk = await Refresh_data(); 
                    Refresh_ui();
                }
            else
                {
                    Refresh_ui();
                }
        isLoaded = true;
         }
 }

async Task<string> Load_Config(string force)
 {
     if (!DeviceNetworkInformation.IsNetworkAvailable)
     {
         MessageBox.Show(RSS.Parameters.MessageNetwork);
         //Load Config from IsoStore
         return "No Network";
     }
     else
     {
         string data = await new WebClient().DownloadStringTaskAsync(url);
         return await GetConfig_Save_Local_XML_Async(data);
         //Load Config from Web, Save to IsoStore
     }
 }

private async Task<string> Refresh_data() //load feeds form Internet and save them to IsoStore
 {
     IsolatedStorageSettings isoStorage = IsolatedStorageSettings.ApplicationSettings;

     if (!DeviceNetworkInformation.IsNetworkAvailable)
     {
         ProgressBarSwitch("off");
         MessageBox.Show(RSS.Parameters.MessageNetwork);

     }
     else
     {
         foreach (RSSFeedInfo sfi in RSS.Parameters.FeedsInfo)
         {
        await Load_Web_XML(new Uri(sfi.Web_XML), sfi.Local_XML);
         }
         isoStorage["SettingsLastUpdate"] = System.DateTime.Now;
         isoStorage.Save();
         ProgressBarSwitch("off");
     }
     return "ok";
 }

private void Refresh_ui() //load feeds from IsoStore
 {
     //use local data
 }

Solution

  • You should always have a try/catch around network code. There are a lot of possible error conditions, of which being offline is only one.

    For example, if you're connected to a local WiFi without Internet, IsNetworkAvailable will return true, even though there's no Internet access.