英文:
C# Orchestration and firestore
问题
以下是您提供的内容的翻译:
我有一个基本的Azure函数,它以一个定时器开始(1分钟用于测试),但经过数小时的努力,我无法从Firestore获取我的数据。
触发器:
[FunctionName("OrchestrationTriggerFunction")]
public async static Task OrchestrationTriggerFunction(
[TimerTrigger("0 */1 * * * *")] TimerInfo myTimer,
[DurableClient] IDurableOrchestrationClient starter)
{
await starter.StartNewAsync("Function1", null);
}
Function1:
[FunctionName("Function1")]
public async Task Function1(
[OrchestrationTrigger] IDurableOrchestrationContext context
)
{
List<string> OrgsToProcess = new List<string>() { "ID" };
foreach (string org in OrgsToProcess)
{
var vehicles = await context.CallActivityAsync<List<Vehicle>>("GetFirestoreVehicles", org);
var parallelTasks = new List<Task<Result>>();
foreach (var vehicle in vehicles)
{
// 处理代码
Task<CsvResult> task = context.CallActivityAsync<Result>("ProcessData", msg);
parallelTasks.Add(task);
}
await Task.WhenAll(parallelTasks);
await context.CallActivityAsync("Function2", parallelTasks);
return;
}
}
GetFirestoreVehicles:
[FunctionName("GetFirestoreVehicles")]
public Task<List<Vehicle>> GetFirestoreVehicles([ActivityTrigger] string org)
{
return _firestoreDb.GetVehicles(org);
}
在Function1中,在 var vehicles = await context.CallActivityAsync<List<Vehicle>>("GetFirestoreVehicles", org);
,我可以看到它具有正确数量的记录,然而所有属性都填充为null。
在调试时查看 GetFirestoreVehicles
时,它显示 Result = "{Not yet computed}"
,我无法在我的Firestore方法中使用 await
,因为它会引发以下异常:
检测到多线程执行。这可能发生在编排器函数代码等待一个不是由DurableOrchestrationContext方法创建的任务的情况下
如何使我的Durable Function 等待来自Firestore的数据?
英文:
I have a basic Azure function that starts with a timmer (1 min for testing), but after hours of trying to make it work, I can't get my data from the firestore.
The trigger:
[FunctionName("OrchestrationTriggerFunction")]
public async static Task OrchestrationTriggerFunction(
[TimerTrigger("0 */1 * * * *")] TimerInfo myTimer,
[DurableClient] IDurableOrchestrationClient starter)
{
await starter.StartNewAsync("Function1", null);
}
Function1:
[FunctionName("Function1")]
public async Task Function1(
[OrchestrationTrigger] IDurableOrchestrationContext context
)
{
List<string> OrgsToProcess = new List<string>() { "ID" };
foreach (string org in OrgsToProcess)
{
var vehicles = await context.CallActivityAsync<List<Vehicle>>("GetFirestoreVehicles", org);
var parallelTasks = new List<Task<Result>>();
foreach (var vehicle in vehicles)
{
// processing code
Task<CsvResult> task = context.CallActivityAsync<Result>("ProcessData", msg);
parallelTasks.Add(task);
}
await Task.WhenAll(parallelTasks);
await context.CallActivityAsync("Function2", parallelTasks);
return;
}
}
GetFirestoreVehicles:
[FunctionName("GetFirestoreVehicles")]
public Task<List<Vehicle>> GetFirestoreVehicles([ActivityTrigger] string org)
{
return _firestoreDb.GetVehicles(org);
}
In Function1, on var vehicles = await context.CallActivityAsync<List<Vehicle>>("GetFirestoreVehicles", org);
I can see that it has the correct number of records, however all the properties are filled with null.
When looking at the GetFirestoreVehicles
during the debugging it says Result = "{Not yet computed}"
and I'm unable to use await
in my firestore method as it throws the following exception:
multithreaded execution was detected. This can happen if the orchestrator function code awaits on a task that was not created by a DurableOrchestrationContext method
How can I make my Durable Function await the data from firestore?
答案1
得分: 2
以下是翻译好的部分:
The GetFirestoreVehicles
function should await the result to function properly like this:
[FunctionName("GetFirestoreVehicles")]
public async Task<List<Vehicle>> GetFirestoreVehicles([ActivityTrigger] string org)
{
return await _firestoreDb.GetVehicles(org);
}
Also, this line is very strange:
await context.CallActivityAsync("Function2", parallelTasks);
You are passing a list of Task<T>
to another function. Instead, you need to pass the resulting list of T
, not the Task
collection itself. Something like this will do:
var results = parallelTasks.Select(t => t.Result);
await context.CallActivityAsync("Function2", results);
That means Function2
should accept IEnumerable<Result>
instead of List<Task<Result>>
.
英文:
The GetFirestoreVehicles
function should await the result to function properly like this:
[FunctionName("GetFirestoreVehicles")]
public async Task<List<Vehicle>> GetFirestoreVehicles([ActivityTrigger] string org)
{
return await _firestoreDb.GetVehicles(org);
}
Also, this line is very strange:
await context.CallActivityAsync("Function2", parallelTasks);
You are passing a list of Task<T>
to another function. Instead, you need to pass the resulting list of T
, not the Task
collection itself. Something like this will do:
var results = parallelTasks.Select(t => t.Result);
await context.CallActivityAsync("Function2", results);
That means Function2
should accept IEnumerable<Result>
instead of List<Task<Result>>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论