【问题标题】:ServiceBus binding breaks Node.js Azure FunctionServiceBus 绑定破坏了 Node.js Azure 函数
【发布时间】:2020-12-08 18:34:28
【问题描述】:

我有一个由 http 触发的简单 azure 函数

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: "Success"
    };
}

函数.json

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

此时我可以触发函数并查看响应。

然后我尝试将服务总线绑定添加到 function.json

{
  "bindings": [
    ...
    {
      "type": "serviceBus",
      "direction": "out",
      "name": "outputSbTopic",
      "topicName": "topicName",
      "connection": "ServiceBusConnection"
    }
  ]
}

当我添加绑定时,函数返回 404 并且日志中没有任何内容。我什至还没有开始使用绑定。

有什么问题吗?我在这个问题上苦苦挣扎了 2 个多小时,没有更多的想法。

host.json(以防万一)

{
  "version": "2.0",
  "extensions": {
    "serviceBus": {
      "prefetchCount": 100,
      "messageHandlerOptions": {
        "autoComplete": true,
        "maxConcurrentCalls": 32,
        "maxAutoRenewDuration": "00:05:00"
      }
    }
  }
}

运行时版本 ~2

Node.js 版本 Node.js 12 LTS

应用程序从包文件以只读模式运行。

AppType 函数AppLinux

更新 我使用 VS Code Azure Function Extension 创建了该函数,并使用 DevOps 进行了部署。后来我在 azure 门户中手动创建了函数。与两个函数的应用服务编辑器文件比较,发现我的第一个函数在 host.json 中没有 extensionBundle。这就是原因。

【问题讨论】:

    标签: node.js azure-functions azureservicebus


    【解决方案1】:

    使用你的 host.json 也会遇到同样的问题:

    问题似乎来自函数应用中的 host.json。

    在我这边,这些文件是:

    index.js

    module.exports = async function (context, req) {
        context.log('JavaScript HTTP trigger function processed a request.');
        var message = "This is a test to output to service bus.";
        context.bindings.testbowman = message;
        context.done();
        context.res = {
            status: 200,
            body: "This is a test to output to service bus topic."
        };
    };
    

    function.json

    {
      "bindings": [
        {
          "authLevel": "anonymous",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "get",
            "post"
          ]
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        },
        {
            "name": "testbowman",
            "type": "serviceBus",
            "topicName": "testbowman",
            "connection": "str",
            "direction": "out"
        }
      ]
    }
    

    host.json

    {
      "version": "2.0",
      "logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          }
        }
      },
      "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"
      }
    }
    

    local.settings.json

    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "",
        "FUNCTIONS_WORKER_RUNTIME": "node",
        "str":"Endpoint=sb://testbowman.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxx="
      }
    }
    

    它奏效了:

    【讨论】:

    • 我使用 VS Code Azure Extension 创建了函数,并使用 DevOps 进行了部署。后来我在 azure 门户中手动创建了函数。对比两个函数的应用服务编辑器文件,发现我的第一个函数没有extensionBundle。这就是原因。回来关闭问题并找到您的答案。我把它标记为正确的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    相关资源
    最近更新 更多