@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.