在我的Angular 2+/ASP.NET Core MVC WebAPI应用程序中,我从客户端UI到服务器进行了长时间运行的API调用. 30秒后,请求超时.我想为此客户端或服务器端的特定API调用定义超时.
In my Angular 2+ / ASP.NET Core MVC WebAPI application I have a long running API-call from the client UI to the server. The request times out after 30 seconds. I would like to define the timeout for this specific API call either client- or server-side.
我只找到了全局配置服务器超时的方法,这不是我想要的,因为30秒的超时对于其他呼叫来说是可以的.我还发现了通过传递RxJS的timeout运算符来减少超时客户端的解决方案.但是,据我了解,这不适合 增加超时时间吗?
I have only found ways to configure the server timeout globally, which is not what I want, as the 30 second timeout is fine for other calls. I've also found solutions to decrease the timeout client side by piping RxJS's timeout operator. However, as I understand, this is not suitable for increasing the timeout?
如何设置此特定请求的超时时间? 30秒默认值是从哪里来的?
How can I set the timeout on this specific request? Where does the 30 second default come from?
(该代码实际上并不重要,因为它是标准的API控制器和标准的Angular HTTP调用):
(The code doesn't actually matter, as it is a standard API controller and a standard Angular HTTP call):
this.http.get('api/xyz').subscribe(d => { ... });推荐答案
要解决超时问题,您可以使用如下所示的RxJ中的timeout
To solve the timeout issue you can use the timeout from RxJs like below
向timeout运算符
这里我要传递60秒的超时时间,因此60秒后如果服务器没有响应,则会抛出超时错误.
Here I am passing 60 seconds as timeout, so after 60 seconds if there is no response from the server then it will throw timeout error.
import ‘rxjs/add/operator/timeout’; ... this.http.get('api/xyz').timeout(60000).subscribe(d => { ... });更多推荐
增加Angular 2+和ASP.NET Core WebAPI应用程序中的超时
发布评论