azure - 如何使用Azure函数Binder查询运行时指定的Table?

标签 azure azure-functions azure-table-storage

通过指定带有表名称的表属性,可以将 Azure 函数绑定(bind)到表,非常简单:

[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 绑定(bind)表:

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

但是IBinder接口(interface)似乎不支持表绑定(bind)来从表中读取数据。我无法将 BindAsync 与 CloudTable 类型一起使用来获取对表的引用。我可以绑定(bind)到 TableEntity,但这仅适用于使用 IAcyncCollector 将数据插入表的目的。

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

最佳答案

如果tableName是示例中所述的Http触发器的路由,则只需使用路由参数来指定绑定(bind)即可。

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

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

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

关于azure - 如何使用Azure函数Binder查询运行时指定的Table?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54688100/

相关文章:

c# - 已引发 Azure MobileServiceInvalidOperation 异常

azure - 尝试使 AAD 和 Azure SQL 身份验证正常工作

azure - 有没有办法让文件在Azure文件存储API中自动增长

c# - Azure Functions 应用程序中多个函数的 Application Insights 警报

azure - 如何在消费模式下分析 .Net Core Azure Function?

c# - Azure.Data.Tables.TableClient 是线程安全的吗?

azure - 为什么更新 aks 集群也会更新节点镜像

node.js - Azure Function v2 文件系统写入不再有效并抛出 4058 无此类文件或目录错误

c# - TableServiceContext 不会序列化 ValueObjects