我正在尝试按照此处介绍的代码https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-eternal-orchestrations?irgwc=1&OCID=AID2000142_aff_7593_1375745&tduid=(ir__6jtlnbrfc0kft3svkk0sohznxn2xi0sgvpzqs0fg00)(7593)(1375745)()()&irclickid=_6jtlnbrfc0kft3svkk0sohznxn2xi0sgvpzqs0fg00&tabs=csharp进行操作,以在持久函数中创建永恒的编排。
当前,我有一个HTTP触发功能
[FunctionName("Trigger_Eternal_Orchestration")]
public static async Task<HttpResponseMessage> OrchestrationTrigger(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage request,
[DurableClient] IDurableOrchestrationClient client)
{
string id = request.Query["Id"];
await client.StartNewAsync("Periodic_Cleanup_Loop", instanceId).ConfigureAwait(false);
return client.CreateCheckStatusResponse(request, instanceId);
}
和编排触发功能为
[FunctionName("Periodic_Cleanup_Loop")]
public static async Task Run(
[OrchestrationTrigger] IDurableOrchestrationContext context)
{
DateTime nextCleanup = context.CurrentUtcDateTime.AddSeconds(10);
await context.CreateTimer(nextCleanup, CancellationToken.None);
Console.WriteLine("Going again");
context.ContinueAsNew(null);
}
现在,我只是试图查看永恒的编排工作(每10秒查看一次打印),但是仅执行一次后,我总是会收到错误消息:
检测到多线程执行。如果协调器功能代码等待不是由DurableOrchestrationContext方法创建的任务,则会发生这种情况。
鉴于业务流程函数只有一个等待状态,并且基于上下文创建的内容,因此我不确定错误是根据消息确定的。
答案 0 :(得分:0)
您的代码与文档基本相同,唯一的区别是您仅等待10秒钟。
持久的协调器功能必须是确定性的(https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-code-constraints),因此很容易发生棘手的错误。
也许使用CreateTimer计划的运行是在当前运行结束之前开始的,这可以解释“多线程”异常。
您可以尝试等待几分钟来更改代码,以验证这一点。 如果您需要更短的时间,则可以改用TimeTrigger或将时间触发和计划的事件组合到队列中(即持久功能下的“秘密”:))。