我使用 VS 17 15.3(带有 nuget 包 Microsoft.NET.Sdk.Functions 1.0.2)和向导创建了一个简单的 HttpTrigger Azure 函数。它给了我以下代码:
public static class Function1
{
[FunctionName("Function1")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "HttpTriggerCSharp/name/{name}")]HttpRequestMessage req, string name, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
// Fetching the name from the path parameter in the request URL
return req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
}
当我使用 VS 在 Debug模式下运行该函数并使用 Postman 调用它时,它工作正常,我有正文响应。
当我使用 CLI: func host start 启动相同的函数并使用 post man 调用它时,该函数不会返回主体。我有一个内容为空的 Http 200。 :(
我发现,在生成的function.json
中,没有http out绑定(bind)。我生成的 function.json
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.0.0",
"configurationSource": "attributes",
"bindings": [
{
"type": "httpTrigger",
"route": "HttpTriggerCSharp/name/{name}",
"methods": [ "get", "post" ],
"authLevel": "anonymous",
"name": "req"
}
],
"disabled": false,
"scriptFile": "..\\bin\\FunctionApp1.dll",
"entryPoint": "FunctionApp1.Function1.Run"
}
当我添加 http 输出绑定(bind)时,使用 func host start 工作正常
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.0.0",
"configurationSource": "attributes",
"bindings": [
{
"type": "httpTrigger",
"route": "HttpTriggerCSharp/name/{name}",
"methods": [ "get", "post" ],
"authLevel": "anonymous", "name": "req"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
],
"disabled": false,
"scriptFile": "..\\bin\\FunctionApp1.dll",
"entryPoint": "FunctionApp1.Function1.Run"
}
很奇怪的是,在 Debug模式下,它可以工作,而不是直接使用cli...
感谢您的帮助
最佳答案
When I start the same function, using the CLI: func host start and calling it with post man, the function doesnt return the body. I have an Http 200 with an empty content
我创建了一个 httpTrigger Azure 函数应用程序,在 Debug模式下运行它时可以获取响应正文。
如果我使用 CLI func host start
来运行函数,我也可以按预期获得响应正文。
请求和响应
此外,根据我的测试(删除 http out 绑定(bind)),如果 function.json 中缺少 http out 绑定(bind),这将导致响应正文中出现空内容。
注意:我这边无法重现该问题(Microsoft Visual Studio Enterprise 2017 Preview (3) Version 15.3.0 Preview 7.0)。但对于这个问题,正如您所做的那样,手动添加 http 输出绑定(bind)将是解决该问题的解决方法。另外,如果可以的话,你可以发个GitHub issue并包含标题“CLI:”。
关于Azure 函数 - VS2017 工具 - function.json 中缺少绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45909870/