Vagif Abilov
Vagif Abilov

Reputation: 9991

How to use Azure functions Binder to query Table specified at runtime?

It's straightforward to bind an Azure function to a table by specifying Table attribute with a table name:

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

But what if the table name is parameterized, i.e. the function reads from table which name is passed in the HTTP request:

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

What I would like to do is to bind a table using TableAttribute:

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

But it doesn't look like IBinder interface supports table binding for the purpose of reading data from a table. I can't use BindAsync with CloudTable type in order to obtain a reference to Table. I can bind to a TableEntity but this works only for the purpose of inserting data to the table using IAcyncCollector.

Is there a way to dynamically bind Azure function to a table by specifying table name at runtime?

Upvotes: 1

Views: 904

Answers (1)

Jerry Liu
Jerry Liu

Reputation: 17800

If the tableName is the route of Http trigger as said in your sample, simply use the route parameter to specify the binding.

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

And I didn't meet any error when using IBinder or Binder

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

Upvotes: 2

Related Questions