我正在开发一个使用 SignalR 触发器的 Azure Functions 项目。我已经设置了函数和配置,但在尝试在本地运行该函数时不断遇到错误。控制台中的错误如下:
[2023-09-06T15:57:30.258Z] Microsoft.Azure.WebJobs.Host:索引方法“Functions.actionReceiver”出错。 System.Collections.Concurrent:值不能为空。 (参数“键”)。 [2023-09-06T15:57:30.278Z]索引方法“Functions.actionReceiver”错误 ... 函数“Functions.actionReceiver”索引失败,将被禁用。
这是我的 SignalR 触发函数的 function.json:
{
"bindings": [
{
"type": "signalRTrigger",
"name": "req",
"hubName": "default",
"event": "clientToServer",
"direction": "in"
},
{
"type": "signalR",
"name": "signalRMessages",
"hubName": "default",
"connectionStringSetting": "AzureSignalRConnectionString",
"direction": "out"
}
]
}
我的local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=signalrfarmstorage;AccountKey=********;EndpointSuffix=core.windows.net",
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureSignalRConnectionString": "Endpoint=https://farmsignalr.service.signalr.net;AccessKey=******;Version=1.0;"
}
}
还有我的 javascript 函数:
module.exports = async function actionReceiver(context, req) {
console.log('SignalR trigger function processed a request.');
context.bindings.signalRMessages = [{
"target": "newMessage",
"arguments": ["Message from Azure Function"]
}];
};
所有名称和设置似乎都是对齐的,但我无法找出导致问题的原因。谁能帮助我理解这个错误的含义以及如何解决它?我无法在没有此错误的情况下启动我的天蓝色功能,并且无法在线找到任何资源。我是 SignalR 新手。非常感谢任何可以提供澄清的人。
我被引导相信我缺少启动时所需的环境变量,但我不知道是哪一个。
我已确保 local.settings.json 中的连接字符串正确。我重新启动了 VScode,以防环境变量不起作用。我什至尝试完全重新创建该功能,但它不起作用。我不确定这个错误来自哪里。我了解 azure 函数的基础知识:过去我经常使用计时器和 http 触发器,但我只是无法让 SignalR 触发器工作。
编辑: 我已将属性“category”:“messages”添加到我的 function.json 中。它不再给出索引错误,但当我在成功建立的连接上在前端运行此代码时,它仍然没有接收到信号:
connection.invoke('clientToServer', '来自客户端的问候!').catch(err => console.error(err));
索引方法“Functions.actionReceiver”出错。 System.Collections.Concurrent:值不能为空。 (参数“键”)。 [2023-09-06T15:57:30.278Z] 索引方法“Functions.actionReceiver”出错...函数“Functions.actionReceiver”索引失败,将被禁用。
我也尝试过同样的方法。我在天蓝色门户中创建了一个 SignalR。检查以下内容。
我正在使用与您类似的代码并做了一些更改。下面的代码在我的本地运行良好。
功能代码:
module.exports = async function (context, req, connectionInfo) {
if (req.query.message) {
const message = req.query.message;
context.bindings.signalRMessages = [{
"target": "newMessage",
"arguments": [message]
}];
context.res = {
status: 200,
body: "Message sent to SignalR."
};
} else {
context.res = {
status: 400,
body: "Please provide a 'message' query parameter."
};
}
};
Function.json:
{
"bindings": [
{
"type": "httpTrigger",
"direction": "in",
"name": "req",
"authLevel": "anonymous",
"methods": ["post"],
"route": "negotiate"
},
{
"type": "signalR",
"direction": "out",
"name": "$return",
"hubName": "default",
"connectionStringSetting": "AzureSignalRConnectionString"
}
]
}
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureWebJobsStorage": "your-azure storage connectionstring",
"AzureSignalRConnectionString":"your-signalR-connectionstring"
}
}
func start
时,我的函数执行成功。
检查以下内容:
这里我可以成功地将消息发送到SignalR。
结果: