I have a webapi assembled with the repository pattern and work unit and I need to make an asynchronous save but I do not get it.
In my service layer I have the following:
List<Task> guardadosAsincronos = new List<Task>();
while (...) {
...
guardadosAsincronos.Add(this.SaveAsync());
...
}
for (var i = 0; i < guardadosAsincronos.Count; i++) {
guardadosAsincronos[i].Wait();
}
Each class in the service layer extends from another base that contains the following function:
public async Task<int> SaveAsync() {
int result = await this._unitOfWork.CommitAsync();
return result;
}
This calls the function of the UnitOfWork class:
public Task<int> CommitAsync() {
return this._databaseFactory.Get().CommitAsync();
}
and in turn, this calls to the context:
public virtual Task<int> CommitAsync() {
return base.SaveChangesAsync(new System.Threading.CancellationToken());
}
When I do the first save, the functions are called one after the other but, when the context function is reached, the execution stops, so instead of an asynchronous save, one becomes synchronous and my loop The main layer of the service layer does not continue until the save ends.
Can someone tell me what I'm doing wrong so that the asynchronous save is not done ???