AZF0002: Inefficient HttpClient usage

This rule is triggered when an HttpClient is instantiated within a [FunctionName]-decorated method.

Value
Rule ID AZF0002
Category [Reliability]
Severity Warning

Rule description

Simple usage of HttpClient to make HTTP requests presents several issues, including vulnerability to socket exhaustion. In a Function app, calling the HttpClient constructor in the body of a function method will create a new instance with every function invocation, amplifying these issues. For apps running on a Consumption hosting plan, inefficient HttpClient usage can exhaust the plan's outbound connection limits.

The recommended best practice is to use an [IHttpClientFactory] with dependency injection or a single static HttpClient instance, depending on the nature of your application.

How to fix violations

  • Move your HttpClient instantiation to a static instance defined outside of the function, such as in this example.

  • Supply an HttpClient to your functions using IHttpClientFactory through dependency injection. This is the recommended approach for fixing this violation.

When to suppress the rule

This rule should not be suppressed.

Next steps

For more information about connection management best practices in Azure Functions, see Manage connections in Azure Functions.

For more information about HttpClient behavior issues and management, see Use IHttpClientFactory to implement resilient HTTP requests