c# - 使用 http 触发的函数将输入绑定(bind)到表存储

标签 c# .net azure azure-functions azure-table-storage

是否可以(输入)绑定(bind)到 http 触发函数中的表存储?

我正在尝试使用以下属性将输入绑定(bind)添加到常规 http 触发函数内的表存储:

    [Table("MyTable", "MyPartition", "{httpTrigger}")] MyPoco poco

但是,当我执行它时,它返回以下错误:

[6/5/2019 5:36:38 PM] An unhandled host error has occurred. [6/5/2019 5:36:38 PM] Microsoft.Azure.WebJobs.Host: 'tableStorageInputBindingHttpTriggered' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes?.

此外,在启动时,我收到此异常:

[6/5/2019 6:17:17 PM] tableStorageInputBindingHttpTriggered: Microsoft.Azure.WebJobs.Host: Error indexing method 'tableStorageInputBindingHttpTriggered'. Microsoft.Azure.WebJobs.Host: Unable to resolve binding parameter 'httpTrigger'. Binding expressions must map to either a value provided by the trigger or a property of the value the trigger is bound to, or must be a system binding expression (e.g. sys.randguid, sys.utcnow, etc.).

完整功能如下:

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("");
    }
}

我做错了什么?如何在 http 触发的 azure 函数中绑定(bind)到表存储?

最佳答案

问题是 http 触发器返回您一个对象,因此它不知道如何提取您的 key 。

你需要使用路由,它会告诉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)

关于c# - 使用 http 触发的函数将输入绑定(bind)到表存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56465532/

相关文章:

c# - LiveSDK 5.6.3 无法在 UWP 商店应用程序中运行

c# - .NET 中程序集的概念是什么?

azure - Azure函数的绑定(bind)可以让消息作为缓冲区传递还是使用特定编码传递

azure - 具有 Azure 身份验证的私有(private) Web 服务器的 SSL 证书

azure - 将应用程序洞察与 Power BI 集成时数据未更新

c# - Winforms Thread Application Hang(后台工作线程)

c# - 如何以编程方式启用\禁用 ToolStripMenuItem 中的嵌套子菜单项?

c# - 如何从代码后面隐藏和显示 asp :buttons in asp.net?

c# - 您如何设置 HTTP 和 HTTPS WCF 4 RESTful 服务?

c# - 使用定时器实现双击事件