gpt4 book ai didi

c# - Azure Durable Function 不创建存储表实体

转载 作者:行者123 更新时间:2023-12-02 08:20:47 26 4
gpt4 key购买 nike

我有一个持久函数,我想在计算结果进入存储队列后立即使用它来聚合它们。另外,我希望该函数在 Azure 存储表上创建一个名为“结果”的表实体。

该函数按预期工作,但没有创建任何单个表实体。但是,当我通过 https://func-foobar.azurewebsites.net/api/create 调用我的测试触发器 TestTrigger 时?创建一个表条目。

这是为什么呢?我在这里做错了什么?

namespace Aggregator
{
public static class AggregatorFunction
{
public class ResultEntity
{
public string PartitionKey { get; set; }

public string RowKey { get; set; }

public uint Run { get; set; }

public decimal IntermediatePi { get; set; }
}

[FunctionName("Counter")]
[return: Table("result")]
public static ResultEntity Counter
(
[EntityTrigger] IDurableEntityContext ctx,
ILogger log
)
{
switch (ctx.OperationName.ToLowerInvariant())
{
case "add":
var unit = ctx.GetInput<UnitOfWork>();
log.LogInformation($"Processing: {unit}");

(decimal IntermediatePi, ulong IntermediateHits, uint run) = ctx.GetState<(decimal, ulong, uint)>();

IntermediateHits += unit.NumHits;
IntermediatePi = (decimal)IntermediateHits / unit.TotalRuns * 4.0m;
run += 1;

log.LogInformation($"==> State: run={run} intermediateHits={IntermediateHits} intermediatePi={IntermediatePi}");
ctx.SetState((IntermediatePi, IntermediateHits, run));

return new ResultEntity
{
PartitionKey = "Pi",
RowKey = Guid.NewGuid().ToString(),
Run = run,
IntermediatePi = IntermediatePi
};

case "delete":
log.LogInformation("Deleting state!");
ctx.DeleteState();

return null;
}

return null;
}

[FunctionName("QueueTrigger")]
public static async Task Run
(
[QueueTrigger(queueName: "results")] string json,
[DurableClient] IDurableEntityClient entityClient
)
{
var unit = JsonSerializer.Deserialize<UnitOfWork>(json);
var entityId = new EntityId("Counter", "CounterKey");
await entityClient.SignalEntityAsync(entityId, "add", unit);
}

[FunctionName("HttpResetTrigger")]
public static async Task Reset
(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "reset")] HttpRequest req,
[DurableClient] IDurableEntityClient entityClient
)
{
var entityId = new EntityId("Counter", "CounterKey");
await entityClient.SignalEntityAsync(entityId, "delete", null);
}

[FunctionName("TestTrigger")]
[return: Table("result")]
public static ResultEntity CreateTable
(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "create")] HttpRequest req
)
{
return new ResultEntity
{
PartitionKey = "Test",
RowKey = Guid.NewGuid().ToString(),
Run = 1,
IntermediatePi = ulong.MaxValue
};
}
}
}

最佳答案

还没有尝试过类似的方法,但文档有这样的说法( https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-bindings?tabs=csharp#trigger-behavior ):

Warning

Orchestrator functions should never use any input or output bindings other than the orchestration trigger binding. Doing so has the potential to cause problems with the Durable Task extension because those bindings may not obey the single-threading and I/O rules. If you'd like to use other bindings, add them to an activity function called from your orchestrator function. For more information about coding constraints for orchestrator functions, see the Orchestrator function code constraints documentation.

因此,这里正确的方法是使用事件函数进行表实体写入。

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

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