【问题标题】:How to use Azure functions Binder to query Table specified at runtime?如何使用 Azure 函数 Binder 查询运行时指定的表?
【发布时间】:2019-02-15 00:10:06
【问题描述】:

通过使用表名指定 Table 属性将 Azure 函数绑定到表很简单:

[FunctionName("TableInput")]
public static void Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, 
    [Table("MyTable")] MyTable table, 
    ILogger log)
{
    ...
}

但是如果表名是参数化的,即函数从表中读取 HTTP 请求中传递的名称:

[FunctionName("queryById")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", Route = "{tableName}")] HttpRequest req,
    Binder binder, 
    ILogger log,
    string tableName)

我想做的是使用 TableAttribute 绑定一个表:

var attributes = new Attribute[]
{    
    new TableAttribute(collectionName), 
};

但是看起来 IBinder 接口不支持表绑定以从表中读取数据。我不能使用 CloudTable 类型的 BindAsync 来获取对 Table 的引用。我可以绑定到 TableEntity,但这仅适用于使用 IAcyncCollector 将数据插入表的目的。

有没有办法通过在运行时指定表名来将 Azure 函数动态绑定到表?

【问题讨论】:

    标签: azure azure-functions azure-table-storage


    【解决方案1】:

    如果tableName是您的示例中所说的Http触发器的路由,只需使用路由参数指定绑定即可。

    [Table("{tableName}")] CloudTable table
    

    而且我在使用IBinderBinder时没有遇到任何错误

    var table = await binder.BindAsync<CloudTable>(new TableAttribute($"{tableName}"));
    

    【讨论】:

    • 谢谢杰瑞。我的代码抱怨将 BindAsync 与 CloudTable 类型一起使用,但显然是出于其他原因(未正确设置存储帐户)。
    猜你喜欢
    • 1970-01-01
    • 2013-04-17
    • 2020-12-12
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多