Home > database >  How to debug locally Azure Function triggered by EventGrid?
How to debug locally Azure Function triggered by EventGrid?

Time:03-14

I create a new Azure Function which I want it to run by EventGrid. I use Visual Studio 2022 Community.

I want to debug the Function by Postman but I couldn't find the way to do it.

My 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
    }
}

When I F5 the project to debug, the console app shows:

Functions:

    Function1: eventGridTrigger

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

I try to post by Postman to the Url:

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

There are two things that happen:

1.The Postman request returns:

cannot find function: 'Function1'

2.The console application shows:

[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] }

This is the post which I read to get the Url for debugging: https://harrybellamy.com/posts/debugging-azure-function-event-grid-triggers-locally/

Is there anything which I'm missing in order to debug the Function by Postman?

Thank you in advanced!

CodePudding user response:

The referenced article says:

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

CodePudding user response:

Have a look at the official Microsoft documentation on how to Manually run a non HTTP-triggered function.

To run a non HTTP-triggered function, you need a way to send a request to Azure to run the function. The URL used to make this request takes a specific form.

Define the request location: host name   folder path   function name

  • Host name: The function app's public location that is made up from the function app's name plus azurewebsites.net or your custom domain.
  • Folder path: To access non HTTP-triggered functions via an HTTP request, you have to send the request through the folders admin/functions.
  • Function name: The name of the function you want to run.

This works both in Azure and local.

Next to this, see Azure Function Event Grid Trigger Local Debugging which takes an alternative approach of running your Function locally and using ngrok to have Event Grid actually call into your locally running Function.

  • Related