添加Service Bus绑定后Node.js Azure Function返回404问题排查
解决Azure Function添加Service Bus输出绑定后返回404的问题
问题背景
你原本有一个运行正常的HTTP触发Azure Function,使用Node.js 12 LTS,运行在Linux函数应用(只读包运行模式),运行时版本为~2。当你在function.json中添加了Service Bus输出绑定(还没在代码中使用这个绑定)后,函数突然开始返回404,而且没有任何日志输出。
你的初始函数代码:
module.exports = async function (context, req) { context.log('JavaScript HTTP trigger function processed a request.'); context.res = { // status: 200, /* Defaults to 200 */ body: "Success" }; }
初始function.json配置:
{ "bindings": [ { "authLevel": "function", "type": "httpTrigger", "direction": "in", "name": "req", "methods": [ "get", "post" ] }, { "type": "http", "direction": "out", "name": "res" } ] }
添加Service Bus绑定后的function.json:
{ "bindings": [ { "authLevel": "function", "type": "httpTrigger", "direction": "in", "name": "req", "methods": [ "get", "post" ] }, { "type": "http", "direction": "out", "name": "res" }, { "type": "serviceBus", "direction": "out", "name": "outputSbTopic", "topicName": "topicName", "connection": "ServiceBusConnection" } ] }
你的host.json初始配置:
{ "version": "2.0", "extensions": { "serviceBus": { "prefetchCount": 100, "messageHandlerOptions": { "autoComplete": true, "maxConcurrentCalls": 32, "maxAutoRenewDuration": "00:05:00" } } } }
问题根源
在Azure Functions 2.x及以上的Linux环境中,尤其是当你的函数应用以只读模式从包文件运行时,函数扩展(比如Service Bus绑定所需的扩展)需要通过extensionBundle来统一管理和加载。你的host.json中缺少这个配置,导致Service Bus绑定的扩展无法被正确加载,进而引发函数应用启动异常,最终表现为HTTP请求返回404且无日志输出。
解决方案
修改你的host.json,添加extensionBundle配置,确保扩展能被正确加载。修改后的host.json示例如下:
{ "version": "2.0", "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[3.*, 4.0.0)" }, "extensions": { "serviceBus": { "prefetchCount": 100, "messageHandlerOptions": { "autoComplete": true, "maxConcurrentCalls": 32, "maxAutoRenewDuration": "00:05:00" } } } }
extensionBundle的id是固定的官方扩展包IDversion指定了扩展包的版本范围,这里选择3.x系列(兼容Functions 2.x+的运行时),你可以根据需要调整为合适的版本范围
配置完成后,重新部署你的函数应用或者重启函数应用,问题即可解决。
内容的提问来源于stack exchange,提问作者Daniil Grankin
相关产品推荐
相关产品推荐

