简体   繁体   English

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

[英]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.

相关问题 检查网格中的事件是否已触发,以及它是否使用C#发送了正确的响应(Azure) - Check if event in grid was triggered and if it sent the correct response (Azure) with C# 如何在事件中心触发的Azure函数中获取PartitionId? - How to get PartitionId in event hub triggered Azure function? Azure function 未由 EventHubTrigger 触发 - Azure function not triggered by EventHubTrigger 对 Azure Function 使用事件网格触发器时,我可以访问绑定表达式中的数据属性吗? - When using Event Grid trigger for Azure Function can I access data properties in binding expressions? 使用 SQL 客户端将时间触发的 Azure Function 与数据库连接 - Connect time-triggered Azure Function with Database using SQL Client 我们如何将事件网格的重试计数传递给 azure function? - How can we pass retry count of event grid into azure function? 如何通过事件触发功能? - How can a function be triggered with an event? azure function - 当 eventthub 中的新事件时触发,将其写入 blob 存储 - 不工作,为什么? - azure function - triggered when new event in eventhub, writing it to blob storage - not working, why? Azure 事件网格以增加侦听器 - Azure event grid to multiply listeners Azure 服务总线触发函数 - 绑定到 MessageReceiver - Azure Service Bus Triggered Function - Bind to MessageReceiver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM