在本地调试 Azure Functions

2024-05-30

我在 .Net 标准 2.0 中有一个函数:

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

根据这篇文章:https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local“非 HTTP 触发函数”部分

“对于除 HTTP 触发器和 Webhook 之外的所有类型的函数,您可以通过调用管理端点来在本地测试您的函数。在本地服务器上使用 HTTP POST 请求调用此端点会触发该函数。您可以选择将测试数据传递给执行在 POST 请求的正文中。此功能类似于 Azure 门户中的“测试”选项卡。

您调用以下管理员端点来触发非HTTP功能:”

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

我应该能够使用以下方法测试非 http 触发的函数:

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

但是,当作为调试运行时,我得到的只是 400 错误:

[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?


这里的问题是curl包含来自的引号--data '{}'在 POST 请求的实际正文中。

Azure 函数需要这种主体:

{}

实际上,curl 发送的是这样的:

'{}'

解决方案是不要对 --data 参数使用引号,如下所示:

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在本地调试 Azure Functions 的相关文章

随机推荐