c# - 如何在本地调试由EventGrid触发的Azure Function?

标签 c# azure azure-functions visual-studio-2022

我创建了一个新的 Azure 函数,希望它由 EventGrid 运行。我使用 Visual Studio 2022 社区。

我想通过 Postman 调试该函数,但找不到方法。

我的 TestFunction.cs:

public static class TestFunction
{
    [FunctionName("Function1")]
    public static async Task Function1([EventGridTrigger] EventGridEvent triggerEvent,
                                              [EventGrid(TopicEndpointUri = "Topic", TopicKeySetting = "TopicKey")] IAsyncCollector<EventGridEvent> outputEvents,
                                              ILogger log)
    {
        //TODO
    }
}

当我 F5 要调试的项目时,控制台应用程序显示:

Functions:

    Function1: eventGridTrigger

For detailed output, run func with --verbose flag.

我尝试通过 postman 发布到以下网址:

http://localhost:7071/runtime/webhooks/EventGrid?functionName=Function1

发生了两件事:

1.Postman请求返回:

cannot find function: 'Function1'

2.控制台应用程序显示:

[2022-03-14T12:20:57.715Z] Executing HTTP request: {
[2022-03-14T12:20:57.717Z]   requestId: "a31453db-3717-43d8-a008-bd459a454fb0",
[2022-03-14T12:20:57.718Z]   method: "POST",
[2022-03-14T12:20:57.718Z]   userAgent: "PostmanRuntime/7.29.0",
[2022-03-14T12:20:57.719Z]   uri: "/runtime/webhooks/EventGrid"
[2022-03-14T12:20:57.721Z] }
[2022-03-14T12:20:57.724Z] cannot find function: 'Function1', available function names: []
[2022-03-14T12:20:57.725Z] Executed HTTP request: {
[2022-03-14T12:20:57.726Z]   requestId: "a31453db-3717-43d8-a008-bd459a454fb0",
[2022-03-14T12:20:57.727Z]   identities: "(WebJobsAuthLevel:Admin, WebJobsAuthLevel:Admin)",
[2022-03-14T12:20:57.728Z]   status: "404",
[2022-03-14T12:20:57.729Z]   duration: "10"
[2022-03-14T12:20:57.729Z] }

这是我阅读的用于获取调试 Url 的帖子:https://harrybellamy.com/posts/debugging-azure-function-event-grid-triggers-locally/

为了调试 Postman 的函数,我是否缺少任何东西?

提前谢谢您!

<小时/>

更新2022年3月15日

当我删除这一行(Function1 中的参数)后它就可以工作,我可以调试 Function1:

[EventGrid(TopicEndpointUri = "MyEventGridTopicUriSetting", TopicKeySetting = "MyEventGridTopicKeySetting")]IAsyncCollector<EventGridEvent> outputEvents,

最佳答案

引用的文章说:

If you try to send the request to the URL now, you will find that it doesn’t work. This is because Event Grid requests require a ‘magic’ header for them to work.

Add the following header to your request in your request generator:

aeg-event-type: Notification

关于c# - 如何在本地调试由EventGrid触发的Azure Function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71467670/

相关文章:

C# Moq 框架返回空引用不正确的对象

c# - 如何在下拉列表中插入项目作为最后一个选项

c# - 如何让 FxCop 理解 'multi-case' 单词?

c# - Google 登录可以在本地主机和测试服务器上使用,但不能在 azure 网站(应用程序服务)上使用

java - 在 Azure Functions 中重用 MongoDB 连接

c# - 允许空外键 GUID

powershell - 如何使用 PowerShell 部署到 Azure 应用服务?

azure - 事件中心 API POST : 40103: Invalid authorization token signature

Azure Functions blob 触发器比 WebJobs blob 触发器更可靠?

node.js - Azure Functions Linux 上的 Puppeteer 引发异常 “Failed to launch the browser process!”