How can I retry connecting to an Azure Mobilservice/or an other server?

Hallo,

i use an Azure Mobilservice to load Objects (Events) from an online database. Everything works fine if I start the App with an active internet-connection. But when I start the App without an internet-connection and try to connect again though an DisplayAlert, after activating the WLAN of my device manuelly, i get everytime a NameResolutionFailture Exeption (https://msdn.microsoft.com/de-de/library/ms346609(v=vs.110).aspx). Only when I click on this DisplayAlert very quickly, it can retrieve the date after a lot clicks. How can I change this?

public static async Task<ObservableCollection> GetEventsAsync()
{

        string ApplicationURL = @"http://blablabla.azurewebsites.net";

        IMobileServiceTable<Event> eventTable;
        MobileServiceClient client;

        client = new MobileServiceClient(ApplicationURL);
        eventTable = client.GetTable<Event>();

        try{
            await Task.Delay(1000);
            IEnumerable<Event> items = await eventTable.ToEnumerableAsync();
            return new ObservableCollection<Event>(items);

        } catch (MobileServiceInvalidOperationException msioe) {
            System.Diagnostics.Debug.WriteLine (@"Invalid sync operation: {0}", msioe.Message);
        } catch (Exception e) {
            System.Diagnostics.Debug.WriteLine (@"Sync error: {0}", e.Message);
        }
        return null;

    }

    public async Task<bool> getOnlineEvents()
    {

        ObservableCollection<Event> o;
        ObservableCollection<Event> ob = events;
        ObservableCollection<Group> ov;
        bool returnbool = false;

        for (int i = 1; i > 0; i++)
        {

            o = await EventManager.GetEventsAsync();

            if (o != null)
            {

                //has internet-connection
                foreach (Event e in o)
                {

                    bool bo = false;

                    foreach (Event ev in events)
                    {
                        if (e.id.Equals(ev.id))
                        {
                            bo = true;
                        }
                    }

                    if (!bo)
                    {
                        ob.Add(e);
                    }

                }

                ob = Methods.sortEvents(ob);
                ov = Methods.groupEvents(ob);

                events = ob;
                groupedEvents = ov;

                absoluteLayout.Children.Remove(activityIndicator);

                EventsList.ItemsSource = null;
                EventsList.ItemsSource = groupedEvents;

                SaveResource.convertEventToPreEvent();
                SaveResource.SaveResourceInXML();

                break;

            }
            else {

                //has no internet-connection
                EventsList.ItemsSource = null;
                EventsList.ItemsSource = groupedEvents;

                if (events.Count == 0)
                {
                    await DisplayAlert("Keine Internetverbindung!", "Eine aktive Internetverbindung wird beim ersten Start der App benötigt.", "Erneut versuchen");
                }
                else {
                    var answerNormalStart = await DisplayAlert("Keine Internetverbindung!", "Events wurden aus dem Speicher geladen.", "Ok", "Erneut versuchen");
                    if (answerNormalStart)
                    {
                        break;
                    }
                }

            }

        }

        return true;

    }

Answers

  • VH5150VH5150 GBMember ✭✭

    Why not try testing for internet connectivity to your Azure site (using the Montemagno connectivity plugin) before calling out to await eventTable.ToEnumerableAsync(); and failing?

    You could handle the network retries using the super simple Polly library?

    Works for me in my Azure Mobile App backend and XF app.

    Just a suggestion.

    Cheers!

Sign In or Register to comment.