Xamarin & RestSharp - Async call hangs/never returns

I am writing an Android app in Xamarin to perform an async json call using restsharp. My setup involves an android java bindings library (jar file converted to c#). This library has one class that subclasses Application and looks something like this

HealthData.java (which is part of jar and referenced into the app as project) Intent is to run this before any activities are loaded.

import android.app.Application;

public class HealthData extends Application {
    private int intTest;

    HealthData () {
        intTest = 0;
    }

    public int getIntTest(){
        return intTest;
    }

    public void setIntTest(int value){
        intTest = value;
    }
}

I am loading this subclass in AndroidManifest.xml as below -

<application android:label=“demo" android:name=“.HealthData">
        <activity android:name=“.MainActivity" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
    </application>

And my mainactivity (written in c#) which performs async call looks like this -

using RestSharp;

namespace demo
{
    [Activity (Label = "demo", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        static string TAG = "PRINT-ME";

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            callme();

            Log.Info (TAG, "callme returns");
        }

        private async void callme()
        {
            // Call the method that runs asynchronously.
            string result = await WaitAsynchronouslyAsync();

            // Display the result.
            TextView txt = FindViewById<TextView> (Resource.Id.textViewAsync);
            txt.Text = result;
        }

        public async Task<string> WaitAsynchronouslyAsync()
        {
            // fetch some json via restsharp
            var client = new RestClient ("http://rxnav.nlm.nih.gov/REST/RxTerms/rxcui/");
            var request = new RestRequest (String.Format ("{0}/allinfo", "198440"));

            Log.Info (TAG, "before awaiting");

            // ExecuteTaskSync
            IRestResponse resp = await ExecuteTaskAsync (client, request);

            return resp.Content;
        }
}

Problem - ExecuteTaskAsync blocks/never returns when I use HealthData in manifest file. If I skip loading HealthData in AndroidManifest.xml, async call returns and everything is fine.

Any idea why the async call fails when I use HealthData? I don't see any errors or warnings from the logcat.

I have tried with c# version of HealthData and used it in Manifest.xml and async call works FINE.

Posts

  • CheesebaronCheesebaron DKInsider, University mod

    You problem is probably a deadlock since it never returns. Try adding ConfigureAwait(false); to the end of your tasks.

    Also that async void method is totally unnecessary and does not bubble up any exceptions, which can cause nasty things. So either make it async Task or catch the possible Exception inside.

Sign In or Register to comment.