skip to Main Content

I have a function, triggered off a service bus. The bus contains data from several sources, which I want to process and write to a storage table. I can’t guarantee there won’t be duplicate entries in the bus, but require unique entries in the table. I’ve figured out I can access an IAsyncCollector of my object type, but this doesn’t have a way to search for objects that already exist, or to update them. Here’s my current function code:

[FunctionName(nameof(Run))]
public async Task Run([Microsoft.Azure.WebJobs.ServiceBusTrigger("myBus", Connection = "myCon")] ServiceBusReceivedMessage message,
    [Table("Widgets", Connection = "AzureWebJobsStorage")] IAsyncCollector<Widgets> widgetsTable,
    ILogger log)
{
    log.LogInformation($"queue trigger function processed message");
            
}

How do I access the table in a way that allows me to find/update objects?

2

Answers


  1. You cannot use the output binding for that:

    This output binding only supports creating new entities in a table. If you need to update an existing entity from your function code, instead use an Azure Tables SDK directly.

    (source)

    So you need to remove the output binding and leverage the table SDK directly.

    Login or Signup to reply.
  2. I do this using a TableClient and let it deal with duplicates. It will either insert or update a row in a table depending on whether it exists or not.

    public static async Task<string> TheFunction(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        [Table("%TableName%", Connection = "StorageConnectionStringAzure")] TableClient tableClient,
        ILogger log)
    {
      ...
      await tableClient.UpsertEntityAsync(theObject, cancellationToken: cancellationToken);
      ...
    }
    

    If you need to know whether what you are updating in the table is the one you want, i.e. perhaps has the latest data for the object, you can use the TableClient directly to query the table to see if what’s already in there is "out of date" etc.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search