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
}
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.
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.
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 = ...,
...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With