Android, Xamarin Forms PCL, PortableRest PCL, and Async Web Api Call

I am trying to use PortableRest to make an Async call in web API 2.2 Rest service from Xamarin Forms.

I think I have some kind of deadlock / sync problem, but I can't handle it (new to async, etc.).

Can anybody help?

My controller check method (any call to the database is deleted) -

public IEnumerable<ContentModel> GetTestRest()
{
    return new List<ContentModel> {
        new ContentModel() {Categoryid = 1, Title = "Title"}};
}

My Unit Test Passes -

[TestMethod]
public async Task TestRest()
{
    MyNewsApiClient MyNewsApiClient = new MyNewsApiClient();

    var models = await MyNewsApiClient.TestRest();
    int count = models.Count;
    Assert.AreEqual(1, count);
}

My PortableRest Proxy (PCL) method is

public async Task<List<ContentModel>> TestRest()
{
    var request = new RestRequest();
    request.Resource = "Content/GetTestRest";

    return await ExecuteAsync<List<ContentModel>>(request);
}

Xamarin Forms ContentPage (PCL) -

public partial class NewsType1CP : ContentPage
{
    public NewsType1CP ()
    {
        InitializeComponent ();
    }

    protected override void OnAppearing ()
    {
        LoadData ();  // this is a sync call of an async method, not sure how else to  approach, make OnAppearing async?
    }

    public async Task LoadData ()
    {
        Debug.WriteLine ("LoadData");

        HeaderLabel.Text = "Load Data!";

        MyNewsApiClient api = new MyNewsApiClient ();

        var cm = await api.TestRest ();
        // this will work on its own, control returns to this method - await api.GetDelay ();

        HeaderLabel.Text = "After! - ";

        NewsListView.ItemsSource = cm;
    }
}

Waiting for api.TestRest () never results in setting HeaderLabel.After or ListView.

If I just add the test method Proxy Method GetDelay (), which does not call PortableRest via return wait ExecuteAsync> (request);

public async Task<bool> GetDelay ()
{
    await Task.Delay (1000);
    return true;
}

Then everything "works."

, PortableRest, ( dll Nuget), , - .

+4
1

, Async ( PortableRest), , , .

try (, !) -

: 'System.Net.Http.HttpClientHandler.set_AutomaticDecompression'.

, -

http://davidburela.wordpress.com/2013/07/12/error-when-using-http-portable-class-library-compression/

PortableRest PCL Forms, Nuget Http Android.

-

: ConnectFailure ( )

, Android, ( , ).

( GenyMotion/Virtual Box), Android LocalHost ( -Api)

, -

http://bbowden.tumblr.com/post/58650831283/accessing-a-localhost-server-from-the-genymotion

, , VirtualBox > > > > VirtualBox > .

.

+8

All Articles