Azure函数HTTP触发器处理30MB请求返回500错误,需支持100MB请求
Azure Functions大请求体返回500错误的解决方法
问题场景
使用HTTP触发器触发Azure函数时,请求体约30MB、URL长度低于4KB时,函数可正常运行,但返回响应或抛出异常时会触发500错误,无法收到响应;请求体≤25MB则可正常工作。报错信息如下:
Failed to proxy request to the worker. Retrying in 50ms. Attempt 1 of 3. Failed to proxy request to the worker. Retrying in 100ms. Attempt 2 of 3. System.Private.CoreLib: Exception while executing function: Functions.function-name. Microsoft.Azure.WebJobs.Script.Grpc: Failed to proxy request with ForwarderError: RequestBodyDestination. One or more errors occurred. (Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host..) (An error occurred while sending the request.). System.Net.Sockets: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.. An existing connection was forcibly closed by the remote host.
解决方案
以下调整可支持处理≤100MB的请求并解决500错误:
配置host.json的HTTP参数
修改项目根目录下的host.json,明确设置请求体限制、并发数和超时时间:{ "version": "2.0", "extensions": { "http": { "requestBodyLimit": 104857600, // 100MB,单位字节 "maxOutstandingRequests": 100, "maxConcurrentRequests": 50 } }, "functionTimeout": "00:10:00" // 延长函数处理超时时间 }调整函数应用的应用程序设置
在Azure门户进入函数应用的配置 > 应用程序设置,添加或修改以下项:FUNCTIONS_GRPC_MAX_MESSAGE_SIZE: 设置为104857600(100MB),解决宿主与worker之间gRPC通信的消息大小限制WEBSITES_CONTAINER_START_TIME_LIMIT: 设置为600(10分钟),给大请求足够的处理启动时间FUNCTIONS_WORKER_PROCESS_COUNT: 设置为2-4,提升worker的并发处理能力WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT: 根据业务需求调整最大实例数,避免单实例过载
代码层面优化
- 采用流式处理请求体:直接读取
HttpRequest.Body流,避免一次性将整个大请求加载到内存中 - 流式返回响应:如果返回大内容,使用
Stream类型的响应结果,而非直接返回大对象,减少内存占用和传输压力
- 采用流式处理请求体:直接读取
内容的提问来源于stack exchange,提问作者VTP
相关产品推荐
相关产品推荐

