简体   繁体   English

如何使用长Web服务异步更新asp.net页

[英]How to update asp.net page asynchronously with a long web service

Here is my simulated process: a web service FindPrimeNumbers(int Num) will find a number of prime numbers equal to the input value. 这是我的模拟过程:Web服务FindPrimeNumbers(int Num)将找到等于输入值的质数。 For example, if (Num = 1000), then FindPrimeNumber(1000) will find the first 1000 primes starting with 1, 2, 3 etc. Whenever a prime number is found, it should update the web page by appending the new prime number to a label. 例如,如果(Num = 1000),则FindPrimeNumber(1000)将查找以1、2、3等开头的前1000个素数。每当找到素数时,应通过将新素数附加到以下来更新网页一个标签。

I am thinking to use .net web api as my service. 我正在考虑使用.net Web API作为我的服务。 What I don't know is how to (raise an event?) update the web page whenever a prime is found. 我不知道什么时候发现素数就如何(引发事件?)更新网页。

Thanks for the help. 谢谢您的帮助。

ajax wouldn't work here - it's either success or error . ajax在这里不起作用- successerror Think about it this way - if your web service was returning json and you flushed a partial response, it would end up looking like {'test': 'fun ; 可以这样考虑-如果您的Web服务返回json并刷新了部分响应,它将最终看起来像{'test': 'fun ; it would be unparsable and of no use. 这将是无法解决的,没有任何用处。

You'd need to use a WebSocket instead as LB mentioned. 您需要使用WebSocket来代替LB所述。 This will allow you to open a connection and raise events on the server and client side between the two as you please. 这将允许您打开连接,并根据需要在两者之间的服务器和客户端上引发事件。 Here's a link with browsers that support. 这是支持的浏览器的链接 (currently Chrome/FF/Safari/IE10+) (当前为Chrome / FF / Safari / IE10 +)

I have thought about something like this before, but I still don't have a chance to actually work on it. 之前我曾考虑过类似的事情,但是我仍然没有机会进行实际的工作。 My suggestion is to have a look at SignalR . 我的建议是看一下SignalR Somehow, you will execute a long-run function in the code behind. 不知何故,您将在后面的代码中执行一个长期运行的函数。 Then it will run and update client everytime there is new result until its done. 然后它将在每次有新结果时运行并更新客户端,直到完成为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM