Xamarin HttpClient方法GetAsync超时错误

时间:2019-01-16 16:22:10

标签: c# api xamarin

我创建了一个API来获取数据,但它显示超时错误。我正在Xamarin的主要功能内部调用该功能,该功能在运行应用程序时被调用。

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

我的API GetAsync调用函数:

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;
        }
    }
}

代码未显示任何错误。当执行转到GetAsync语句时,它会等待一段时间并发生异常。

System.Net.WebException: The request timed out. ---> Foundation.NSErrorException: Exception of type 'Foundation.NSErrorException' was thrown.

2 个答案:

答案 0 :(得分:2)

考虑将异步事件处理程序与静态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;
}

使用此模式有助于避免阻塞调用和套接字耗尽,这有时会导致死锁,从而导致超时。

引用Async/Await - Best Practices in Asynchronous Programming

引用You're using HttpClient wrong

答案 1 :(得分:0)

尝试一下。

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;
    }
}