【发布时间】:2019-01-31 18:02:12
【问题描述】:
当我使用func new --name MyHttpTrigger --template "HttpTrigger" 添加新函数时,此函数中没有创建function.json,当我尝试将其添加到当前目录并运行func start --build 时,我收到此错误:
未找到工作职能。尝试公开您的工作类别和方法。如果您正在使用绑定扩展(例如 Azure 存储、ServiceBus、计时器等),请确保您已在启动代码中调用了扩展的注册方法(例如 builder.AddAzureStorage()、builder.AddServiceBus( )、builder.AddTimers() 等)。
你可以在这里找到我的function.json 内容:
{
"disabled": false,
"bindings": [
{
"authLevel": "anonymous",
"name": "req",
"type": "httpTrigger",
"direction": "in"
},
{
"name": "res",
"type": "http",
"direction": "out"
}
]
}
以前的httpTrigger函数
namespace final
{
public static class httpTrigger
{
[FunctionName("httpTrigger")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
}
新的httpTrigger函数
namespace final
{
public static class httpTrigger
{
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
}
【问题讨论】:
-
C#(注意不是 C# 脚本)函数的 function.json 文件应该由构建过程生成,因此您不必手动添加。你能澄清一下你在哪里添加function.json文件(它和你的host.json在同一个目录吗?别处?),什么时候? (预构建?您没有在 /bin/output/MyHttpTrigger/ post-build 中看到 function.json 并且正在添加它吗?)
-
当我使用 C# 使用
func new不带 --csx 参数创建函数时,我尝试在host.json附近的 function.json 附近添加函数,我也尝试向此添加新目录函数并在run.cs附近添加function.json(因为默认情况下没有为函数创建目录),但是当我使用--csx参数时,新目录是用function.json创建的...... -
感谢您提供此信息。回复为答案。
标签: c# azure azure-functions serverless