Azure Functions中的HttpClient最佳实践 [英] HttpClient best practices in Azure Functions

查看:71
本文介绍了Azure Functions中的HttpClient最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要构建一个Azure功能,

I need to build an Azure Function that:

  • 响应HTTP POST请求
  • 根据数据执行7个HTTP HEAD请求.
  • Responds to a HTTP POST request
  • Does 7x HTTP HEAD requests based on the data.

我在此处此处

但是,目前尚不清楚该怎么做以及它如何工作?

But, it's not entirely clear what to do, and how it works?

与第二个链接一样,我目前刚刚声明了一个private static HttpClient httpClient = new HttpClient();实例,并在我的7x HTTP HEAD调用中重复使用了该实例.

As in the second link, i've currently just declared a private static HttpClient httpClient = new HttpClient(); instance, and re-used that in my 7x HTTP HEAD calls.

我的问题:

  1. 在无状态Azure函数中,HttpClient的使用效率最高吗?
  2. 我目前正在为http调用建立List<Task>(),然后对它们进行Task.WhenAll(tasks)使其并行运行.那将是拨打这些电话的最快方法吗?还有其他建议吗?
  1. Is that the most efficient use of HttpClient in a stateless Azure function?
  2. I'm currently just building up a List<Task>() for the http calls, then doing Task.WhenAll(tasks) on them to run them in parrallel. Would that be the quickest way to do these calls? Any other suggestions?

此Function端点将被调用很多次(每秒多次),因此需要尽可能高效以降低成本.

This Function endpoint will be called a lot (multiple times a second), so need to be as efficient as possible to keep costs down.

谢谢!

推荐答案

是的-这仍然是Azure Functions 1.x(也适用于2.x)的当前指南,以最好地避免套接字耗尽.静态变量将确保将其与该类的所有实例共享.涵盖此主题的另一篇好文章是 https://aspnetmonsters.com/2016/08/2016-08-27-httpclient错误

Yes - this is still the current guidance for Azure Functions 1.x (and applies to 2.x as well) to best avoid socket exhaustion. The static variable will ensures that it will be shared against all instances of the class. Another good article that covers this topic is https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong

这篇关于Azure Functions中的HttpClient最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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