Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function into Table Storage

I have an Azure Function and I want to have it taking message from the EventHub (This is fairly straightforward 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 output isn't null, I don't know what I need for an OperationContext or if calling ITableEntity.Write() is even correct way to get it to write to the table storage.

ETA Json Binding:

{
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "eventHubMessage",
      "direction": "in",
      "path": "measurements",
      "connection": "MeasurementsConnectionString"
    }
  ],
  "disabled": false
}
like image 364
Stuart Avatar asked Mar 21 '17 10:03

Stuart


People also ask

Does Azure function need storage account?

Storage account requirementsWhen creating a function app, you must create or link to a general-purpose Azure Storage account that supports Blob, Queue, and Table storage. This requirement exists because Functions relies on Azure Storage for operations such as managing triggers and logging function executions.

Where are Azure Functions stored?

IMPORTANT] When using the Consumption/Premium hosting plan, your function code and binding configuration files are stored in Azure Files in the main storage account.


1 Answers

To add a new entry to Table you should bind to IAsyncCollector instead of entity itself, then create a new entity 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 = ...,
    ...
});
like image 139
Mikhail Shilkov Avatar answered Sep 18 '22 04:09

Mikhail Shilkov