Azure 函数在本地工作但在上传到 azure 后不起作用

问题描述

我是 azure 函数和 azure 的新手。

我正在使用 azure 函数与 SignalR 服务进行交互,以便在 xamarin 表单应用程序中使用。我从文档中借用了 azure 函数代码MSDocs on serverless signalR service

在本地运行良好(测试了文档中提供的 Web 客户端,以及一个简单的控制台应用程序)。

但是当函数被移动到 azure 时。最初我遇到 CORS 错误修复了这些错误,然后遇到了 502。无法调试或找到根本原因。经过几个小时的浏览发现azure本身提供了signalR无服务器连接的模板。

enter image description here

使用模板,使用 signalR 端点配置应用设置(我已经为 AzureSignalRConnectionString 设置了应用设置)。

仍然面临502错误,。 我怎样才能让它工作?或者我如何找出失败的根本原因。

协商功能代码

index.js

module.exports = async function (context,req,connectionInfo) {
    context.res.body = connectionInfo;
};

函数.json

{
  "bindings": [
    {
      "authLevel": "anonymous","type": "httpTrigger","direction": "in","methods": [
        "post"
      ],"name": "req"
    },{
      "type": "http","direction": "out","name": "res"
    },{
      "type": "signalRConnectionInfo","name": "connectionInfo","hubName": "messaage","connectionStringSetting": "AzureSignalRConnectionString","direction": "in"
    }
  ]
}

解决方法

当您使用 SignalR 服务触发器绑定时,

SignalR 服务需要一个 URL 来访问 Function App。 enter image description here URL 格式:<Function_App_URL>/runtime/webhooks/signalr?code=<API_KEY>。说明:Function_App_URL 可以在 Function App 的概览页面上找到,API_KEY 是由 Azure Function 生成的。您可以在 Function App 的 App keys 刀片中从 signalr_extension 获取 API_KEY。

如果您对此非常陌生,请阅读以下分步文章:https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/BidirectionChat