I wrote a method that populates a list with objects returned by an asynchronous method.
public async Task<ObjectToReturn> GetObjectToReturnAsync()
{
var objectToReturn = new ObjectToReturn
{
List1 = [
await AsyncMethod1("A", null),
],
List2 = [
await AsyncMethod1("B", false),
await AsyncMethod1("C", false),
await AsyncMethod1("D", false),
await AsyncMethod1("E", false),
],
List3 = [
await AsyncMethod1("B", true),
await AsyncMethod1("C", true),
await AsyncMethod1("D", true),
await AsyncMethod1("E", true),
]
};
return objectToReturn;
}
private async Task<ObjectInList> AsyncMethod1(string status, bool? reopened)
{
var total = await repository.GetCountByStatusAsync(status, reopened);
return new ObjectInList(status, total);
}
and here is the method in the repository
public async Task<int> GetCountByStatusAsync(string status, bool? reopened = null)
{
var query = context.Table1.AsQueryable();
// if (reopened != null)
// query = query.Where(c => c.reopened);
var count = await query.CountAsync(c => c.status == status);
return count;
}
here the exceution log
[2024-09-06T07:14:02.302Z] Executed DbCommand (51ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.303Z] SELECT count(*)::int
[2024-09-06T07:14:02.304Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.305Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.312Z] Executed DbCommand (2ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.313Z] SELECT count(*)::int
[2024-09-06T07:14:02.314Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.314Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.314Z] Executed DbCommand (1ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.316Z] SELECT count(*)::int
[2024-09-06T07:14:02.317Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.318Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.321Z] Executed DbCommand (1ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.322Z] SELECT count(*)::int
[2024-09-06T07:14:02.323Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.324Z] Executed DbCommand (1ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.324Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.328Z] SELECT count(*)::int
[2024-09-06T07:14:02.328Z] Executed DbCommand (2ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.329Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.331Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.331Z] Executed DbCommand (1ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.330Z] SELECT count(*)::int
[2024-09-06T07:14:02.335Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.334Z] Executed DbCommand (1ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.336Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.333Z] SELECT count(*)::int
[2024-09-06T07:14:02.337Z] Executed DbCommand (2ms) [Parameters=[@__status_0='?'], CommandType='Text', CommandTimeout='30']
[2024-09-06T07:14:02.338Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.340Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.339Z] SELECT count(*)::int
[2024-09-06T07:14:02.337Z] SELECT count(*)::int
[2024-09-06T07:14:02.342Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.341Z] FROM schema1.table1 AS c
[2024-09-06T07:14:02.344Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.344Z] WHERE c.status = @__status_0
[2024-09-06T07:14:02.432Z] Error, Self referencing loop detected for property 'target' with type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[ObjectToReturn,Class1+<GetObjectToReturnAsync>d__2]'. Path 'moveNextAction'.
[2024-09-06T07:14:02.464Z] Title : Invalid data processException : Self referencing loop detected for property 'target' with type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[ObjectToReturn,Class1+<GetObjectToReturnAsync>d__2]'. Path 'moveNextAction'. Inner Exception :Stack Trace : at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)
it seems that the last calls are not executed sequentially.
Why this problem and how can I solve?
sorry for the first poor post. I mistakenly assumed that the information provided might be sufficient
5
I created a fiddle showing that the methods are executed in order as expected: https://dotnetfiddle.net/PC8Xo0.
The output is:
A
B False
C False
D False
E False
F True
G True
H True
I True
This demonstrates that the async methods are awaited in the order they are written.
The issue you mentioned might be elsewhere in the code.