1

i am trying to make an asynchronous call to a webservice. I would like to make this call when opening the app (App.xaml.cs). According to the answer that comes back to me, it has to navigate to a particular page But I do not work.

public partial class App : PrismApplication
{
    public App(IPlatformInitializer initializer = null) : base(initializer) { }

    protected override void OnInitialized()
    {
        InitializeComponent();
        try
        {
            CheckLogin().Wait();


        }
        catch (Exception e)
        {
            var t = e;
        }
    }
    private static async Task CheckLogin()
    {


        try
        {
            var login = new Login
            {
                Email = "[email protected]",
                Password = "test",
            };
            var client = new HttpClient { BaseAddress = new Uri("http://www.api.com/test/") };
            var data = JsonConvert.SerializeObject(login);

            var content = new StringContent(data, Encoding.UTF8, "application/json");
            var response = await client.PostAsync(@"api/it-IT/auth/token", content);  //crash without error, freeze
            if (response.IsSuccessStatusCode)
            {
                var successResult = JsonConvert.DeserializeObject<HttpResponseMessage>(response.Content.ReadAsStringAsync().Result);
                if (successResult != null)
                {
                    //return true;
                }
                else
                {
                    //return false;
                }
            }


        }
        catch (Exception e)
        {
            var t = e;
        }



    }
    protected override void RegisterTypes()
    {
        Container.RegisterTypeForNavigation<NavigationPage>();
        Container.RegisterTypeForNavigation<MainPage>();
        Container.RegisterTypeForNavigation<MainPage2>();
        Container.RegisterTypeForNavigation<MainPage3>();
    }


}

When does the postasync call does not go more forward, not I get no errors, but does not proceed.

But if I try the same code in an application console, everything works fine, why?

  class Program
{

        static void Main(string[] args)
        {
            Console.WriteLine("A");

            CheckLogin().Wait();

            Console.WriteLine("K");
            Console.ReadKey();
        }


    private static async Task CheckLogin()
    {


        try
        {
            var login = new Login
            {
                Email = "[email protected]",
                Password = "@test",
            };
            var client = new HttpClient { BaseAddress = new Uri("http://www.api.com/test/") };
            var data = JsonConvert.SerializeObject(login);

            var content = new StringContent(data, Encoding.UTF8, "application/json");
            var response = await client.PostAsync(@"api/it-IT/auth/token", content);
            if (response.IsSuccessStatusCode)
            {

            }


        }
        catch (Exception e)
        {
            var t = e;
        }



    }
}

If I try to do the same operation within a command with wait I do not work the same error, but if I do await, it will work fine, but in App.xaml.cs in OnInitialized() I can not put await

     public DelegateCommand callCommand { get; set; }
        public MainPage2ViewModel()
        {
            callCommand = new DelegateCommand(Call);
        }

        private  void Call()
        {
            //await CheckLogin(); // work 
            CheckLogin().Wait(); // not work the same problem
            var i = "pippo";
        }
        private async Task CheckLogin()
        {
         ....
        }

Is there anything to set with xamarin or with prism?

1 Answer 1

1

I've also the same strange error... i fix with this workaround (use an async void that wrap async task)...

public App()
{
    InitializeComponent();
    Current.MainPage = new LoadingPage();
}

protected override void OnStart()
{
    MagicInit();
    base.OnStart();
}

public static async void MagicInit()
{
    var f = await FileSystem.Current.LocalStorage.CreateFileAsync("db.sqlite", CreationCollisionOption.OpenIfExists);
    DbConnection = f.Path;
    await DataService.DbFill();
    User = await DataService.Instance.Table<SpUser>().FirstOrDefaultAsync();
    Current.MainPage = User != null ? (Page)new MainPage() : new LoginPage();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.