1

I have created an api to get data but it is showing timeout error. I am calling the function inside main function of Xamarin that is called when app is run.

public MainPage()
    {
        InitializeComponent();
        //this.BindingContext = new PatientViewModel();
        Task<PatientModel> abc = GetPatientData();
    }

my function for api GetAsync call:

public async Task<PatientModel> GetPatientData()
    {
        PatientModel patient = null;
        try
        {
            Uri weburl = new Uri("myuri");
            HttpClient client = new HttpClient();
            Console.WriteLine("a");
            HttpResponseMessage response = await client.GetAsync(weburl);
            Console.WriteLine("b");
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("in");
                patient = await response.Content.ReadAsAsync<PatientModel>();
                Console.WriteLine("in funciton");
                return patient;
            }
            return patient;
        }catch(Exception ex)
        {
            Console.WriteLine(ex);
            return patient;
        }
    }
}

Code is not showing any error. When execution went to GetAsync statement it waits for a while and exception occurs.

System.Net.WebException: The request timed out. ---> Foundation.NSErrorException: Exception of type 'Foundation.NSErrorException' was thrown.
9
  • have you checked whether or not the server is reachable and responding within the timeout window? Commented Jan 16, 2019 at 16:23
  • you mean the api url? api url is working perfectly. I have tested it in postman where I get prefect result. Commented Jan 16, 2019 at 16:25
  • What are you doing with Task<PatientModel> abc after the constructor? Commented Jan 16, 2019 at 16:28
  • 1
    is it reachable from the device or emulator? Commented Jan 16, 2019 at 16:28
  • 1
    @LamaMadan it is an NSErrorException, which means it is backed by an native NSError, what is that error and stacktrace? Commented Jan 16, 2019 at 16:53

2 Answers 2

2

Consider using an async event handler along with a static HttpClient

static HttpClient client = new HttpClient();

public MainPage() {
    InitializeComponent();
    loadingData += onLoadingData;        
}

protected override void OnAppearing() {
    //loadingData -= onLoadingData; //(optional)
    loadingData(this, EventArgs.Empty);
    base.OnAppearing();
}

private event EventHandler loadingData = delegate { };

private async void onLoadingData(object sender, EventArgs args) {
    var model = await GetPatientData();
    this.BindingContext = new PatientViewModel(model);
}

public async Task<PatientModel> GetPatientData() {
    PatientModel patient = null;
    try {
        Uri weburl = new Uri("myuri");
        Console.WriteLine("a");
        var response = await client.GetAsync(weburl);
        Console.WriteLine("b");
        if (response.IsSuccessStatusCode) {
            Console.WriteLine("in");
            patient = await response.Content.ReadAsAsync<PatientModel>();
            Console.WriteLine("in funciton");
        }           
    }catch(Exception ex) {
        Console.WriteLine(ex);
    }
    return patient;
}

Using this pattern can help avoid blocking calls and socket exhaustion that can at times lead to deadlocks that could cause the timeouts experienced.

Reference Async/Await - Best Practices in Asynchronous Programming

Reference You're using HttpClient wrong

Sign up to request clarification or add additional context in comments.

Comments

0

Try this.

public PatientModel abc { get; set; }

public MainPage()
{
    InitializeComponent();

    Bridge();

    // Using abc
}

public async void Bridge()
{
    abc = new PatientModel();
    abc = await GetPatientData();
}

public async Task<PatientModel> GetPatientData()
{
    PatientModel patient = null;
    try
    {
        Uri weburl = new Uri("myuri");
        HttpClient client = new HttpClient();
        Console.WriteLine("a");
        HttpResponseMessage response = await client.GetAsync(weburl);
        Console.WriteLine("b");
        if (response.IsSuccessStatusCode)
        {
            Console.WriteLine("in");
            patient = await response.Content.ReadAsAsync<PatientModel>();
            Console.WriteLine("in funciton");
            return patient;
        }
        return patient;
    }catch(Exception ex)
    {
        Console.WriteLine(ex);
        return patient;
    }
}

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.