简体   繁体   English

如何在.net core中递归调用web api?

[英]How to call the web api recursively in .net core?

I have an endpoint which returns the response containing hotels and a flag which shows more results are available, the client needs to call this endpoint recursively till the time the server returns more results flag as false.我有一个端点,它返回包含酒店的响应和一个显示更多可用结果的标志,客户端需要递归调用这个端点,直到服务器返回更多结果标志为假。 What is the better way to implement this?实现这一点的更好方法是什么? Could anyone help me on this?有人可以帮我吗?

First Option: Avoid It If Possible第一种选择:尽可能避免它

Please try to avoid calls on HTTP APIs so as to avoid network latency.请尽量避免调用 HTTP API 以避免网络延迟。 This is very important if you want to make multiple calls from a client which is supposed to be responsive.如果您想从一个应该响应的客户端进行多次调用,这非常重要。

eg if you are developing a web application / WPF application and you want user to click on something which triggers 10-20 calls to API, the operation may not complete quickly may result in poor user experience.例如,如果您正在开发 Web 应用程序/WPF 应用程序,并且您希望用户单击触发 10-20 次 API 调用的内容,则操作可能无法快速完成,从而导致用户体验不佳。

If it is a background job, then probably it multiple calls would make more sense.如果它是后台作业,那么多次调用可能更有意义。

Second Option: Optimize HTTP Calls From Client第二个选项:优化来自客户端的 HTTP 调用

If you still want to make multiple calls over HTTP, then you will have to somehow optimize the code in such a way that at least you avoid the network latency.如果您仍然想通过 HTTP 进行多次调用,那么您必须以某种方式优化代码,至少可以避免网络延迟。

For avoiding network latency, you can bring all the data or major chunk of the data in one call on the client side.为了避免网络延迟,您可以在客户端的一次调用中将所有数据或主要数据块带入。 Then client can iterate over this set of data.然后客户端可以迭代这组数据。

Even if you reduce half of the calls you buy much more time for client processing.即使您减少了一半的呼叫,您也为客户处理购买了更多的时间。

Another Option另外一个选项

You can also try to think if this can be a disconnected operation - client sending just one notification to server and then server performing all iterations.您还可以尝试考虑这是否可能是一个断开连接的操作——客户端只向服务器发送一个通知,然后服务器执行所有迭代。

Client can read status somewhere from database to know if this operation is complete.客户端可以从数据库的某处读取状态以了解此操作是否完成。 That way your client UI would still say responsive and you will be able to offload all heavy processing to Server.这样你的客户端 UI 仍然会说响应式,你将能够将所有繁重的处理卸载到服务器。

You will have to think and which of these options suits High Level Design of your product/project.您必须考虑这些选项中的哪些适合您的产品/项目的高级设计。

Hope I have given enough food for thoughts (although this may not be solving your issue directly).希望我已经为思考提供了足够的食物(尽管这可能无法直接解决您的问题)。

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

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