Function App Dependency Error when Sending Http Requests

Roy Zeng 80 Reputation points
2024-11-27T18:52:47.1866667+00:00

Hello, I encountered problems with outgoing requests to dependencies. My server got System.Net.Http.HttpRequestException when I was sending HttpRequest using IHttpClientFactory.CreateClient().SendAsync() and "cancelled" when I was connecting to Cosmos DB. Such an issue only happens when I used flex-consumption servers (it is totally normal when I was using consumption plan or local machine with identical code, environment variables, and app configurations), so I would like to ask if there are any settings in host.json or flex-consumption server settings could lead to such errors?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,185 questions
{count} votes

Accepted answer
  1. JananiRamesh-MSFT 28,871 Reputation points
    2024-12-02T03:56:30.8866667+00:00

    @Roy Zeng Thanks for the update! Glad to see you were able to resolve your issue. Thanks for posting your solution so that others experiencing the same thing can easily reference this.

    Since the Microsoft Q&A community has a policy that the question author cannot accept their own answer, they can only accept answers by others, I'll repost your solution in case you'd like to Accept the answer.

    Issue: You encountered problems with outgoing requests to dependencies on flex-consumption servers. Specifically, experienced System.Net.Http.HttpRequestException when sending HTTP requests using IHttpClientFactory.CreateClient().SendAsync() and "cancelled" errors when connecting to Cosmos DB. These issues did not occur on the consumption plan or local machines with identical code, environment variables, and app configurations. The root cause was identified as the servers continuously sending outgoing requests to Cosmos DB even without incoming requests, leading to socket exhaustion.

    Resolution: The issue was resolved by registering the Cosmos DB client as a singleton. This change ensured that the same instance of the client was reused for multiple requests, preventing socket exhaustion and stabilizing the outgoing requests.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.