l--*_*''' 3 .net c# azure azure-table-storage azure-functions
是否可以(输入)绑定到 http 触发的函数中的表存储?
我正在尝试将输入绑定添加到具有以下属性的常规 http 触发函数内的表存储:
[Table("MyTable", "MyPartition", "{httpTrigger}")] MyPoco poco
Run Code Online (Sandbox Code Playgroud)
但是,当我执行它时,它返回以下错误:
[6/5/2019 5:36:38 PM] 发生了未处理的主机错误。[6/5/2019 5:36:38 PM] Microsoft.Azure.WebJobs.Host:无法从 Azure WebJobs SDK 调用“tableStorageInputBindingHttpTriggered”。是否缺少 Azure WebJobs SDK 属性?。
此外,在启动时,我收到此异常:
[6/5/2019 6:17:17 PM] tableStorageInputBindingHttpTriggered:Microsoft.Azure.WebJobs.Host:错误索引方法“tableStorageInputBindingHttpTriggered”。Microsoft.Azure.WebJobs.Host:无法解析绑定参数“httpTrigger”。绑定表达式必须映射到触发器提供的值或触发器绑定到的值的属性,或者必须是系统绑定表达式(例如 sys.randguid、sys.utcnow 等)。
这是完整的功能:
public class MyPoco
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Directory { get; set; }
}
public static class tableStorageInputBindingHttpTriggered
{
[FunctionName("tableStorageInputBindingHttpTriggered")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[Table("MyTable", "MyPartition", "{httpTrigger}")] MyPoco poco,
ILogger log)
{
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($"PK={poco.PartitionKey}, RK={poco.RowKey}, Text={poco.Directory}")
: new BadRequestObjectResult("");
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?如何绑定到 http 触发的 azure 函数中的表存储?
问题是 http 触发器会返回一个对象,因此它不知道如何提取您的密钥。
您需要使用路由,它将告诉 Function 如何获取参数,然后您将能够使用该参数
public static async Task<HttpResponseMessage> SetLatestAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "release-set-latest/{program}")]
HttpRequestMessage req,
string program,
[Table(TableName, "latest", "{program}")]FlymarkLatestVersion pocos)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1778 次 |
| 最近记录: |