我有一个由事件网格触发的Azure函数(C#)。我很难在本地调试我的函数,因为它只能通过事件网格来触发。
我不确定是否可以将事件网格连接到我的Function的本地主机地址?我尝试使用Web Hook,但它仅支持本地主机使用HTTP的HTTPS。
public static void Run([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
{
ID = eventGridEvent.Subject;
log.LogInformation($"ID: {ID.ToString()}");
}
任何建议都值得赞赏。
答案 0 :(得分:6)
我发现这篇博客文章在本地调试事件网格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"
}
]
答案 1 :(得分:2)
实际上,有一种使用 ngrok 作为隧道的简便方法。因此,您基本上可以创建一个EventGrid订阅,将您的事件发布到ngrok隧道(例如https://ec291dd9.ngrok.io/runtime/webhooks/EventGrid?functionName=myfunc),然后开始在Visual Studio中调试功能。您根本不需要更改功能。
Microsoft也对此文件进行了详细记录:Azure Function Event Grid Trigger Local Debugging
答案 2 :(得分:0)
您是对的。目前,Event Grid的工具正在改进,没有简单的方法可以在本地对其进行测试。
在“事件网格函数”中,我将逻辑与触发器部分分开,通过这种方式,我可以对逻辑进行单元测试,而无需测试真实事件。之后,我将通过Application Insights和一个log.LogInformation(eventGridEvent.Data.ToString());