调试使用事件网格触发的 Azure 函数

Cra*_*son 5 c# azure azure-functions

我有一个由事件网格触发的 Azure 函数 (C#)。我在本地调试我的函数有困难,因为它只能通过事件网格触发。

我不确定是否有办法将事件网格连接到我的函数的本地主机地址?我曾尝试使用 Web Hook,但它仅支持我的本地主机使用 HTTP 的 HTTPS。

public static void Run([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
{
 ID = eventGridEvent.Subject;
 log.LogInformation($"ID: {ID.ToString()}");
}
Run Code Online (Sandbox Code Playgroud)

任何建议表示赞赏。

小智 11

我发现这篇博文帮助我在本地调试事件网格 azure 函数时解决了这个问题:https : //blog.mcilreavy.com/articles/2018-12/debug-eventgrid-triggered-azure-function

在我的案例中缺少的一个部分是 aeg-event-type 标头。 aeg-event-type: Notification

添加后,我可以使用这个 url 在本地进行调试: http://localhost:7071/runtime/webhooks/EventGrid?functionName=nameOfYourFunction

并在请求正文中包含此事件 json,如下所示:

[ { "id": "'1", "eventType": "yourEventType", "eventTime":"10:59:00.000", "subject": "subject1", "data":{"make": "Ducati", "model": "Monster"}, "dataVersion": "1.0" } ]


Mar*_*ndl 4

实际上,有一种很好且简单的方法可以使用ngrok作为隧道来完成此操作。因此,您基本上创建了一个 EventGrid 订阅,将您的事件发布到您的 ngrok 隧道(例如https://ec291dd9.ngrok.io/runtime/webhooks/EventGrid?functionName=myfunc)并开始在 Visual Studio 中调试您的函数。您根本不需要更改您的功能。

Microsoft 也详细记录了此方法:Azure Function Event Grid Trigger Local Debugging