gpt4 book ai didi

c# - Azure Durable 函数 - CallActivityAsync 时出现 InvalidOperationException

转载 作者:行者123 更新时间:2023-11-30 15:54:48 29 4
gpt4 key购买 nike

我正在玩Azure Durable functions 。目前,在调用事件后,我在 Orchestration 函数中收到 InvalidOperationException 。它提示检测到多线程执行。如果协调器函数之前从不支持的异步回调恢复,则可能会发生这种情况。

有人遇到过这样的问题吗?我做错了什么?完整代码可以在GitHub上找到

这是编排函数中的代码行:

var res = await ctx.CallActivityAsync<int>("LengthCheck", "inputData");

LengthCheck 事件函数是:

[FunctionName("LengthCheck")]
public static Task<int> Calc([ActivityTrigger] string input)
{
var task = Task.Delay(TimeSpan.FromSeconds(5));
task.Wait();
return Task.FromResult(input.Length);
}

堆栈跟踪是:

ac6fd5cdd07a4dc9b2577657d65c4f27: Function 'InpaintOrchestration (Orchestrator)', version '' failed with an error. Reason: System.InvalidOperationException: Multithreaded execution was detected. This can happen if the orchestrator function previously resumed from an unsupported async callback.

at Microsoft.Azure.WebJobs.DurableOrchestrationContext.ThrowIfInvalidAccess()

at Microsoft.Azure.WebJobs.DurableOrchestrationContext.d__47`1.MoveNext()

End of stack trace from previous location where exception was thrown

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

最佳答案

只要协调器函数以不受支持的方式执行异步工作,就会发生此异常。在这种情况下,“不支持”实际上意味着 await 用于非持久任务(“非持久”意味着它是来自 IDurableOrchestrationContext< 之外的某个 API 的任务)/)。

您可以在此处找到有关协调器函数的代码约束的更多信息:https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-code-constraints .

以下是我快速扫描您的代码时发现的违反规则:

  • Orchestrator code should be non-blocking. For example, that means no I/O and no calls to Thread.Sleep or equivalent APIs. If an orchestrator needs to delay, it can use the CreateTimer API.

  • Orchestrator code must never initiate any async operation except by using the IDurableOrchestrationContext API. For example, no Task.Run, Task.Delay or HttpClient.SendAsync. The Durable Task Framework executes orchestrator code on a single thread and cannot interact with any other threads that could be scheduled by other async APIs.

当我们检测到进行了不受支持的异步调用时,就会特别发生此异常。我注意到这段代码中发生了这种情况:

    private static async Task SaveImageLabToBlob(ZsImage imageLab, CloudBlobContainer container, string fileName)
{
var argbImage = imageLab
.Clone()
.FromLabToRgb()
.FromRgbToArgb(Area2D.Create(0, 0, imageLab.Width, imageLab.Height));

using (var bitmap = argbImage.FromArgbToBitmap())
using (var outputStream = new MemoryStream())
{
// modify image
bitmap.Save(outputStream, ImageFormat.Png);

// save the result back
outputStream.Position = 0;
var resultImageBlob = container.GetBlockBlobReference(fileName);
await resultImageBlob.UploadFromStreamAsync(outputStream);
}
}

进行异步或阻塞调用的正确方法是将它们包装在没有任何这些约束的事件函数中。

在此扩展的最新版本(v1.3.2 及更高版本)中,我们在异常消息中包含了描述代码约束的文档链接。

关于c# - Azure Durable 函数 - CallActivityAsync 时出现 InvalidOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49477619/

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