gpt4 book ai didi

c# - Azure Function 到表存储

转载 作者:太空狗 更新时间:2023-10-29 23:20:32 29 4
gpt4 key购买 nike

我有一个 Azure 函数,我想让它从 EventHub 获取消息(这相当简单并且有效),然后在运行时使用表绑定(bind)将该信息放入表存储中。

这是我到目前为止所拥有的:

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;
}
}

我的问题;

1) 输出始终为空。

2)即使输出不为空,我也不知道OperationContext需要什么,也不知道调用ITableEntity.Write()是否是让它写入表存储的正确方法。

ETA Json 绑定(bind):

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

最佳答案

要向表中添加新条目,您应该绑定(bind)到 IAsyncCollector 而不是实体本身,然后创建一个新实体并调用 AddAsync。以下代码片段对我有用:

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

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

关于c# - Azure Function 到表存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42924982/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com