[英]Debug an Azure Function that is triggered using an Event Grid
I have an Azure Function (C#) that is triggered by an Event Grid.我有一个由事件网格触发的 Azure Function (C#)。 I am having difficulty debugging my Function locally as it can only be triggered via the Event Grid.
我在本地调试 Function 时遇到困难,因为它只能通过事件网格触发。
I'm not sure if there is a way to wire the Event Grid up to my local host address for my Function?我不确定是否有办法将事件网格连接到我的 Function 的本地主机地址? I have tried using a Web Hook but it only support HTTPS which my localhost uses HTTP.
我尝试使用 Web Hook,但它只支持 HTTPS,我的本地主机使用 HTTP。
public static void Run([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
{
ID = eventGridEvent.Subject;
log.LogInformation($"ID: {ID.ToString()}");
}
Any advice is appreciated.任何建议表示赞赏。
I found this blog post that helped me resolve this when debugging event grid azure function locally: https://blog.mcilreavy.com/articles/2018-12/debug-eventgrid-triggered-azure-function我发现这篇博客文章在本地调试事件网格 azure function 时帮助我解决了这个问题: https://blog.mcilreavy/debugure-eventgrid2/
One piece that was missing in my case was the aeg-event-type header.我的案例中缺少的一件是 aeg-event-type header。
aeg-event-type: Notification
After adding it, I was able to debug locally using this url: http://localhost:7071/runtime/webhooks/EventGrid?functionName=nameOfYourFunction
添加后,我能够使用此 url:
http://localhost:7071/runtime/webhooks/EventGrid?functionName=nameOfYourFunction
在本地调试
And include this event json in the request body, something like this:并在请求正文中包含此事件 json,如下所示:
[ { "id": "'1", "eventType": "yourEventType", "eventTime":"10:59:00.000", "subject": "subject1", "data":{"make": "Ducati", "model": "Monster"}, "dataVersion": "1.0" } ]
Actually, there is a nice and easy way of doing this using a ngrok as a tunnel.实际上,使用ngrok作为隧道有一种很好且简单的方法。 So you basically create a EventGrid Subscription that publishes your events to your ngrok tunnel (eg https://ec291dd9.ngrok.io/runtime/webhooks/EventGrid?functionName=myfunc ) and start debugging your function in Visual Studio.
因此,您基本上创建了一个 EventGrid 订阅,将您的事件发布到您的 ngrok 隧道(例如https://ec291dd9.ngrok.io/runtime/webhooks/EventGrid?functionName=myfunc )并开始在 Visual Studio 中调试您的 function。 You don't need to change your function at all.
您根本不需要更改 function。
This approach is also well documented from Microsoft: Azure Function Event Grid Trigger Local Debugging这种方法也被 Microsoft 详细记录: Azure Function 事件网格触发本地调试
You're right.你是对的。 At this moment, tooling for Event Grid is being improved and there's no easy way to test it locally.
目前,Event Grid 的工具正在改进中,没有简单的方法在本地对其进行测试。
In my Event Grid Functions, I'm separating the logic from the trigger part, this way I can unit test the logic without the need of a real event in order to test it.在我的事件网格函数中,我将逻辑与触发器部分分开,这样我就可以对逻辑进行单元测试,而无需真实事件来测试它。 After that, I'm capturing the event through Application Insights and a
log.LogInformation(eventGridEvent.Data.ToString());
之后,我通过 Application Insights 和
log.LogInformation(eventGridEvent.Data.ToString());
捕获事件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.