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 ();
}
public async Task LoadData ()
{
Debug.WriteLine ("LoadData");
HeaderLabel.Text = "Load Data!";
MyNewsApiClient api = new MyNewsApiClient ();
var cm = await api.TestRest ();
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), , - .