Azure function for storing tables

I have an Azure function and I want it to accept a message from the EventHub (it's pretty simple and works) and then put that information into table storage using table binding at runtime.

Here's what I have so far:

public static async Task Run(string eventHubMessage, TraceWriter log, Binder binder)
{
   var m = JsonConvert.DeserializeObject<Measurement>(eventHubMessage);
   var attributes = new Attribute[]
    {
        new StorageAccountAttribute("AzureWebJobsTest"),
        new TableAttribute(tableName, m.PartitionKey, m.RowKey)
    };

    using(var output = await binder.BindAsync<MyTableEntity>(attributes)) 
    {
        if(output == null)
           log.Info($"4. output is null");
        else
        {
            output.Minimum = m.Minimum;
            output.Maximum = m.Maximum;
            output.Average = m.Average;
            output.Timestamp = m.Timestamp;
            output.ETag = m.ETag;  

            output.WriteEntity(/* Need an operationContext*/)
        }
    }
}
public class MyTableEntity : TableEntity, IDisposable
{
    public double Average { get; set;}
    public double Minimum { get; set;}
    public double Maximum { get; set;}

    bool disposed = false;
    public void Dispose()
    { 
        Dispose(true);
        GC.SuppressFinalize(this);           
    }

   protected virtual void Dispose(bool disposing)
   {
      if (disposed)
         return; 

      if (disposing) 
      {
      }

      disposed = true;
   }
}

      

My problems;

1) The output is always null.

2) Even if the output is not null, I don't know what I need for the OperationContext or if you call ITableEntity.Write (), this is even the correct way to force it to write to the table store.

ETA Json Binding:

{
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "eventHubMessage",
      "direction": "in",
      "path": "measurements",
      "connection": "MeasurementsConnectionString"
    }
  ],
  "disabled": false
}

      

+3


source to share


1 answer


To add a new record to the table, you must bind to IAsyncCollector

instead of the entity itself, then create a new object and call AddAsync

. The following snippet works for me:



var attributes = new Attribute[]
{
    new StorageAccountAttribute("..."),
    new TableAttribute("...")
};

var output = await binder.BindAsync<IAsyncCollector<MyTableEntity>>(attributes);     
await output.AddAsync(new MyTableEntity()
{
    PartitionKey = "...",
    RowKey = "...",
    Minimum = ...,
    ...
});

      

+4


source







All Articles