Debug Azure Functions locally

2019-06-07 17:53发布

问题:

I have a function in .Net standard 2.0:

    [FunctionName("A_Test")]
    public static async Task<string> Test(ILogger log, ExecutionContext context)
    {
        log.LogInformation("test");
        return "hello";
    }

according to this article: https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local section "Non-HTTP triggered functions"

"For all kinds of functions other than HTTP triggers and webhooks, you can test your functions locally by calling an administration endpoint. Calling this endpoint with an HTTP POST request on the local server triggers the function. You can optionally pass test data to the execution in the body of the POST request. This functionality is similar to the Test tab in the Azure portal.

You call the following administrator endpoint to trigger non-HTTP functions:"

 http://localhost:{port}/admin/functions/{function_name}

I should be able to test non-http triggered functions by using:

curl --request POST -H "Content-Type:application/json" --data '{}' http://localhost:7071/admin/functions/A_Test -v

However when running as debug, all I get is a 400 error:

[08/03/2019 12:59:08] Host lock lease acquired by instance ID '000000000000000000000000BED482F9'.
[08/03/2019 12:59:09] Executing HTTP request: {
[08/03/2019 12:59:09]   "requestId": "6dba82a1-65bf-4e10-bcc2-1e7ecdb3524c",
[08/03/2019 12:59:09]   "method": "POST",
[08/03/2019 12:59:09]   "uri": "/admin/functions/A_Test"
[08/03/2019 12:59:09] }
[08/03/2019 12:59:10] Executed HTTP request: {
[08/03/2019 12:59:10]   "requestId": "6dba82a1-65bf-4e10-bcc2-1e7ecdb3524c",
[08/03/2019 12:59:10]   "method": "POST",
[08/03/2019 12:59:10]   "uri": "/admin/functions/A_Test",
[08/03/2019 12:59:10]   "identities": [
[08/03/2019 12:59:10]     {
[08/03/2019 12:59:10]       "type": "WebJobsAuthLevel",
[08/03/2019 12:59:10]       "level": "Admin"
[08/03/2019 12:59:10]     },
[08/03/2019 12:59:10]     {
[08/03/2019 12:59:10]       "type": "WebJobsAuthLevel",
[08/03/2019 12:59:10]       "level": "Admin"
[08/03/2019 12:59:10]     }
[08/03/2019 12:59:10]   ],
[08/03/2019 12:59:10]   "status": 400,
[08/03/2019 12:59:10]   "duration": 614
[08/03/2019 12:59:10] }

Why?

回答1:

Your Function doesn't have any trigger associated with it, that's probably it isn't identified as a trigger. You could add a timer trigger for example.

The following approach/hack might be applicable as well: When I have a non-HTTP trigger and want to test it locally, I extract the actual logic into its own class. Then I open up a separate HTTP trigger that is used just for testing.



回答2:

The problem here is that curl includes the quotes from --data '{}' in the actual body of the POST request.

Azure function expects this kind of body:

{}

And in reality curl sends this:

'{}'

The solution is not to use quotes for the --data argument like so:

curl --request POST -H "Content-Type:application/json" --data {} http://localhost:7071/admin/functions/A_Test -v