Azure Function应用服务计划中ServicePointManager.DefaultConnectionLimit设置位置
ServicePointManager.DefaultConnectionLimit in Azure Functions (App Service Plan) Great question! When running Azure Functions on an App Service Plan (non-consumption), you need to set this static property early in your application's lifecycle—before any outbound HTTP connections are made. Here's where and how to do it:
Primary Placement: Application Startup Code
The best spot is in your function app's initialization logic, which runs once when the app starts up (not per function execution). This ensures the setting applies globally to all functions in the app.
For .NET 6+ (Isolated Worker Model or In-Process)
If you're using the modern .NET stack, add the setting in your Program.cs when configuring the host:
var host = new HostBuilder() .ConfigureFunctionsWorkerDefaults() .ConfigureServices(services => { // Adjust the value based on your outbound connection needs System.Net.ServicePointManager.DefaultConnectionLimit = 100; }) .Build(); host.Run();
For .NET Framework (Legacy In-Process)
If you're on .NET Framework, place the setting in Global.asax.cs's Application_Start method:
protected void Application_Start() { System.Net.ServicePointManager.DefaultConnectionLimit = 100; // Other app initialization code here }
Why Not Set It in Individual Functions?
Avoid setting this property inside a specific function's execution logic. Since ServicePointManager is static, changes made mid-execution might not apply to already-established connections, and could lead to inconsistent behavior across function invocations. Setting it at startup guarantees the value is applied before any outbound HTTP calls are initiated.
Key Note
Remember, this setting controls the maximum number of concurrent connections to a single HTTP server. Adjust the value based on your app's outbound traffic patterns—don't just set it to an arbitrarily high number without testing, as it could lead to resource contention.
内容的提问来源于stack exchange,提问作者JeffC

