异步Http调用Web服务来加载数据而不挂起UI线程 [英] Asynchronous Http calls to web service to load data without suspending the UI thread

查看:53
本文介绍了异步Http调用Web服务来加载数据而不挂起UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net网页,它会调用Web服务来获取数据。我遇到的问题是查询耗时30(s)+ :(我的第一个想法是对服务进行一些异步调用以获取数据,然后缓存
它。


因此,对服务的所有HTTP请求都需要< 20(s)。获取此数据然后显示在图表中。例如

 protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
LoadDefaultChart();
}
}

private void LoadDefaultChart()
{

//调用Web服务(REST)http:// localhost:5454 / api / Trending?chart = devices

//反序列化JSON响应
//绑定到图表
}

//页面加载后如何对Web进行更多异步调用service?




在IsPostBack中执行异步调用会暂停加载UI。这不是我想要做的。在backgro中运行的新线程当退出Page_Load();

解决方案

的范围时,IsPostBack中的und将终止该线程您可能只想直接从JavaScript代码执行REST调用,而不是从服务器后面的代码。


如果* *需要从服务器完成然后将代码放在UpdatePanel中,不要进行Web服务调用,让请求完成,然后立即通过更新面板执行异步回发,使用javascript单击按钮(或其他)
,然后在回发中处理您的Web服务调用。


I have an asp.net web page that makes some calls to a web service to obtain data. The problem I have is that the queries are time consuming 30(s)+ :( My first thought is to make some asynchronous calls to the service to obtain the data, and then cache it.

So of all the HTTP requests to the service one takes < 20(s). This data is obtained and then displayed in a chart. eg.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
       LoadDefaultChart();
    }
}

private void LoadDefaultChart()
{

    // make call to web service (REST) http://localhost:5454/api/Trending?chart=devices

    // deserialze JSON response
    // bind to chart
}

// after the page is loaded how can make more asynchronous calls to web service?


Doing the asynchronous calls in the IsPostBack will suspend the UI from loading. which is not what I want to do. Creating a new thread that runs in the background in the IsPostBack will terminate the thread when exiting the scope of Page_Load();

解决方案

You probably want to just do the REST call from JavaScript code directly, rather than from the code behind on the server.

If it *does* need to be done from the server then put the code in an UpdatePanel, don't do your web service calls, let the request complete, and then immediately do an async postback through the update panel by using javascript to click a button (or whatever) and then handle your web service calls in the post back.


这篇关于异步Http调用Web服务来加载数据而不挂起UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆