Azure functions 如何对Azure HTTP触发器函数进行编码,以将部分传入消息传递给服务总线队列?
我正在设置一些Azure HTTP触发器函数来接收JSON数据 我希望访问并将一些数据向前传递到服务总线队列,以便进行下游处理 我已经能够将出站绑定添加到服务总线,但是在没有得到500内部服务器错误的情况下,无法放置代码以将来自HTTp触发器函数的消息放置在队列上。这是由于函数的异步性质造成的吗 如何实现call-HTTP-Trigger-that-passes-data-forwards-to-a-ServiceB总线队列功能Azure functions 如何对Azure HTTP触发器函数进行编码,以将部分传入消息传递给服务总线队列?,azure-functions,Azure Functions,我正在设置一些Azure HTTP触发器函数来接收JSON数据 我希望访问并将一些数据向前传递到服务总线队列,以便进行下游处理 我已经能够将出站绑定添加到服务总线,但是在没有得到500内部服务器错误的情况下,无法放置代码以将来自HTTp触发器函数的消息放置在队列上。这是由于函数的异步性质造成的吗 如何实现call-HTTP-Trigger-that-passes-data-forwards-to-a-ServiceB总线队列功能 module.exports = async function (
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个内部服务器错误
以下是文件在monent中的外观:
function.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"
}
]
}
{
"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"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "node",
"str":"Endpoint=sb://testbowman.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxx="
}
}
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."
};
};
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",
"iotcwebhookdispatchersbns_SERVICEBUS": "Endpoint=sb://iotc-webhook-dispatcher-sbns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=MiIv52YQ6plMVt35vPvx9U5DmsQbTvUXamaeZ/fA2Eg="
}
}
配置出站服务总线队列时,我发现从现有名称空间中选择服务总线队列名称空间很奇怪。在下一步中,我无法选择实际队列,即使之前已经创建了观测sbq
输入其他队列名称(如testqueue)也不起作用。它没有被创建,并且发生相同的500内部服务器错误,即使是在门户中直接测试功能时也是如此
错误消息:
这是否意味着没有使用/部署local.settings.json文件
日志文件摘录:
2020-08-28T07:03:46.194[信息]JavaScript HTTP触发器函数处理了一个请求。
2020-08-28T07:03:46.249[错误]执行“函数.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[错误]执行“函数.iotc webhook dispatcher”(失败,Id=222a675f-8b16-4720-b807-bcc9834d7ddc,持续时间=1067ms)
Microsoft Azure WebJobs SDK ServiceBus连接字符串“iotcwebhookdispatchersbns_ServiceBus”缺失或为空。以下代码在我这边可以正常工作: function.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"
}
]
}
{
"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"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "node",
"str":"Endpoint=sb://testbowman.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxx="
}
}
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."
};
};
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
"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"
}
]
}
{
"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"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "node",
"str":"Endpoint=sb://testbowman.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxx="
}
}
然后,您将发现一条消息进入服务总线队列:
看来你的代码没有问题,你能显示你的function.json文件吗?你是在本地还是在azure上运行代码?@tobiasperson所以你在azure上运行?@tobiasperson它应该有一些错误输出,你能展示它吗?我在Visual Studio代码中编写代码并将其上传到现有的函数App/Function。我将文件的内容添加到上面的原始帖子中。我看不出它们有什么奇怪的地方,现在你们的代码和我的代码有什么不同了。“选择现有服务总线队列”问题如何?@TobiasPersson Ok,请检查详细信息错误。首先,转到
https://yourfunctionappname.scm.azurewebsites.net/DebugConsole
,然后单击LogFiles\Application\Functions\Function\yourtriggername>
,您将在那里找到日志文件。