通过指定带有表名称的表属性,可以将 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
并且我在使用IBinder
或Binder
时没有遇到任何错误
var table = await binder.BindAsync<CloudTable>(new TableAttribute($"{tableName}"));
关于azure - 如何使用Azure函数Binder查询运行时指定的Table?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54688100/