首页 > 解决方案 > 如何编写将部分传入消息传递给服务总线队列的 Azure HTTP 触发器函数?

问题描述

我正在设置一些 Azure HTTP 触发器函数来接收 JSON 数据。

我想访问将一些数据向前传递到服务总线队列以进行下游处理。

我已经能够将出站绑定添加到服务总线,但无法放置代码以从 HTTp 触发器函数将消息放置在队列上,而不会出现 500 内部服务器错误。这是由于函数的异步性质吗?

如何实现 call-HTTP-Trigger-that-passes-data-onwards-to-a -ServiceB-Bus-Queue 功能?

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

    if (req.query.name || (req.body && req.body.name)) {

        context.log('Incoming data from Webhook' + req);
        context.bindings.outputSbMsg = "Message placed on Service Bus Queue";
        context.log('Message placed on Service Bus Queue');

        context.res = {
            // status: 200, /* Defaults to 200 */
            body: "Hello " + (req.query.name || req.body.name)
        };
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
};

我确实尝试像您一样进行部署,但不幸的是,即使只是运行 GET 查询,我仍然会收到 500 Internal Server Error。

以下是当前文件的样子:

函数.json

  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "serviceBus",
      "direction": "out",
      "connection": "iotcwebhookdispatchersbns_SERVICEBUS",
      "name": "outboundSBQMsg",
      "queueName": "observations-sbq"
    }
  ]
}

index.js

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    var message = "This is a test SBQ message - from an Azure function!";
    context.bindings.outboundSBQMsg = message;
    context.res = {
        status: 200,
        body: "This code is to send message to service bus."
    };
};

local.settings.json 文件

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "iotcwebhookdispatchersbns_SERVICEBUS": "Endpoint=sb://iotc-webhook-dispatcher-sbns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=MiIv52YQ6plMVt35vPvx9U5DmsQbTvUXamaeZ/fA2Eg="
  }
}

在配置出站服务总线队列时,我发现可以从现有命名空间中选择服务总线队列命名空间很奇怪。我无法在下一步中选择实际的队列,即使之前已经创建了 observations-sbq。

输入其他队列名称(如 testqueue)也不起作用。它没有被创建,并且发生同样的 500 Internal Server 错误,即使直接在门户中测试该功能也是如此。

错误消息:

在此处输入图像描述

这是否意味着尚未使用/部署 local.settings.json 文件?

日志文件摘录:

2020-08-28T07:03:46.194 [信息] JavaScript HTTP 触发器函数处理了一个请求。2020-08-28T07:03:46.249 [错误] 执行“Functions.iotc-webhook-dispatcher”(失败,Id=e248ae01-7a82-485e-bcfe-00ebee6496e0,持续时间=14ms)Microsoft Azure WebJobs SDK ServiceBus 连接字符串“iotcwebhookdispatchersbns_SERVICEBUS ' 缺失或为空。2020-08-28T07:07:08.256 [信息] 执行“Functions.iotc-webhook-dispatcher”(原因=“此函数是通过主机 API 以编程方式调用的。”,ID=222a675f-8b16-4720-b807-bcc9834d7ddc) 2020-08-28T07:07:08.818 [信息] JavaScript HTTP 触发器函数处理了一个请求。2020-08-28T07:07:10.107 [错误] 执行“Functions.iotc-webhook-dispatcher”(失败,Id=222a675f-8b16-4720-b807-bcc9834d7ddc,

标签: azure-functions

解决方案


下面的代码在我这边工作正常:

函数.json

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

index.js

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

local.settings.json

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

然后你会发现一条消息进入服务总线队列:

在此处输入图像描述


推荐阅读