EventGrid 订阅绑定通过 ngrok 绑定到本地机器,而不是 Azure 中的函数

EventGrid subscription binding works via ngrok to a local machine, not with a function in Azure

我有一个 Azure Function App,当它通过 ngrok 托管在开发机器上时,我可以创建一个 EventGrid 订阅,但是完全相同的函数,当发布到 Azure 时,将不接受订阅。

错误是"The attempt to validate the provided endpoint <> failed"。

我已经考虑过像 urls 的长度这样明显的事情,但这似乎不是原因。两个 url 之间的唯一区别是主机,如函数应用程序的主机 url 和 ngrok 生成的主机。我已尝试使用门户和 CLI 来创建具有相同结果的订阅。

有什么解决办法吗?

有两种可用于事件网格订阅者的 azure 函数触发器,例如:

  1. HttpTrigger

    endpointUrl format = https://{functionApp}.azurewebsites.net/api/{functionName}?code={functionKey}
    

请注意,HttpTrigger 函数负责验证事件网格消息。查看更多详细信息 here

  1. EventGridTrigger

    endpointUrl format = https://{functionApp}.azurewebsites.net/admin/extensions/EventGridExtensionConfig?functionName={functionName}&code={_masterKey}
    

看起来,您的端点不包含 url 查询字符串。

答案是在云中访问 EventGrid 订阅者时包含代码参数,而不是在开发机器上。详情在这里。 https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-grid#create-a-subscription

上面的文字似乎是所有文档,并没有说明这是特别需要的。

我在 powershell 中使用 Azure CLI 时遇到了同样的问题。对我来说,答案基本上是 Roman Kiss 的评论:Escape the endpoint URL,因为它包含一个 & 符号。

例如,改变这个:

  -endPoint "https://xxxx.azurewebsites.net/runtime/webhooks/EventGrid?FunctionName=xxxx&code=xxxxx"

对此:

  -endPoint "https://xxxx.azurewebsites.net/runtime/webhooks/EventGrid?functionName=xxxx""&""code=xxxx"

它使用 ngrok 工作的原因是 ngrok 端点(指向本地开发功能主机)不需要 &code=xxxx 部分。