Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Azure Mobile Services from Xamarin.Forms?

I try to use the Azure Mobile Services from my simple Forms app and it don't work. The last command just run forever. I checked the internet connection with a WebClient and it is okay. The code works fine in a simple Windows console application.

    var ms = new MobileServiceClient(
            @"https://xxx.azure-mobile.net/",
            @"xxx"
        );

    IMobileServiceTable<TodoItem> todoTable =
        ms.GetTable<TodoItem>();
    List<TodoItem> items = todoTable
        .Where(todoItem => todoItem.Complete == false).ToListAsync().Result;

Edit: I use Xamarin Studio because I only have indie license. For the debug I tried a Samsung Galaxy S3 and a Huawei Ascend P7, the result was same. I have updated the Xamarin and now it produces an interesting exception if I use async-await to get the result.

Java.Lang.RuntimeException: java.lang.reflect.InvocationTargetException
  at --- End of managed exception stack trace ---
  at java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
  at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:675)
  at at dalvik.system.NativeStart.main(Native Method)
  at Caused by: java.lang.reflect.InvocationTargetException
  at at java.lang.reflect.Method.invokeNative(Native Method)
  at at java.lang.reflect.Method.invoke(Method.java:515)
  at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:859)
  at ... 2 more
  at Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
  at at Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnPrepareOptionsMenu (Android.Views.IMenu) <IL 0x00007, 0x00050>
  at Android.App.Activity.n_OnPrepareOptionsMenu_Landroid_view_Menu_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:4151
  at at (wrapper dynamic-method) object.fcd55d8e-49be-4c8f-b3a6-37be6bdb988f (intptr,intptr,intptr) <IL 0x00017, 0x0005b>
  at at md5530bd51e982e6e7b340b73e88efe666e.FormsApplicationActivity.n_onPrepareOptionsMenu(Native Method)
  at at md5530bd51e982e6e7b340b73e88efe666e.FormsApplicationActivity.onPrepareOptionsMenu(FormsApplicationActivity.java:110)
  at at android.app.Activity.onPreparePanel(Activity.java:2612)
  at at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:518)
  at at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:872)
  at at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:243)
  at at android.view.Choreographer$CallbackRecord.run(Choreographer.java:780)
  at at android.view.Choreographer.doCallbacks(Choreographer.java:593)
  at at android.view.Choreographer.doFrame(Choreographer.java:561)
  at at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:766)
  at at android.os.Handler.handleCallback(Handler.java:733)
  at at android.os.Handler.dispatchMessage(Handler.java:95)
  at at android.os.Looper.loop(Looper.java:136)
  at at android.app.ActivityThread.main(ActivityThread.java:5290)
  at ... 5 more
like image 373
Kiss László Avatar asked Apr 27 '15 20:04

Kiss László


1 Answers

Are you sure you are not blocking your UI with a combination of async / await (deadlock)? Modify your code as follows:

IMobileServiceTable<TodoItem> todoTable = await ms.GetTable<TodoItem>();
List<TodoItem> items = todoTable.Where(todoItem => todoItem.Complete == false).ToListAsync().ConfigureAwait(false);

See here for more Information about this specific problem: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

For further informations about a deadlock, Stephen Clary has an excellent blog about this: http://blog.stephencleary.com/2012/02/async-and-await.html

like image 112
Florian Moser Avatar answered Nov 15 '22 19:11

Florian Moser