Azure 中托管的 SignalR 函数建立了连接,但不触发 SignalRTrigger 函数

问题描述 投票:0回答:1

我正在开发一个使用 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));

javascript azure azure-functions signalr
1个回答
0
投票

索引方法“Functions.actionReceiver”出错。 System.Collections.Concurrent:值不能为空。 (参数“键”)。 [2023-09-06T15:57:30.278Z] 索引方法“Functions.actionReceiver”出错...函数“Functions.actionReceiver”索引失败,将被禁用。

  • 配置所有必需的设置均已正确定义,包括与 SignalR 触发器相关的配置设置,例如 Azure SignalR 连接字符串、集线器名称或其他自定义设置。

我也尝试过同样的方法。我在天蓝色门户中创建了一个 SignalR。检查以下内容。

enter image description here

我正在使用与您类似的代码并做了一些更改。下面的代码在我的本地运行良好。

功能代码:

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
    时,我的函数执行成功。 检查以下内容: enter image description here

这里我可以成功地将消息发送到SignalR。

enter image description here

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.