Richiediamo migliaia di oggetti indietro da Dapper e raggiungiamo il limite di parametro (2100), quindi abbiamo deciso di caricarli in blocchi.Utilizzo di Async e Attendi per interrompere la chiamata al database (con Dapper)
Ho pensato che sarebbe stata una buona occasione per provare Async Await - questa è la prima volta che ho avuto un tentativo così magari facendo un errore da ragazzino!
I punti di interruzione sono stati colpiti, ma il tutto non sta semplicemente tornando. Non si tratta di un errore: sembra proprio che tutto vada in un buco nero!
Aiuto per favore!
Questo è stato il mio metodo originale - ora chiama il metodo asincrono
public List<MyObject> Get(IEnumerable<int> ids)
{
return this.GetMyObjectsAsync(ids).Result.ToList();
} //Breakpoint on this final bracket never gets hit
ho aggiunto questo metodo per suddividere gli id in blocchi di 1000 e poi aspettano per i compiti da completare
private async Task<List<MyObject>> GetMyObjectsAsync(IEnumerable<int> ids)
{
var subSets = this.Partition(ids, 1000);
var tasks = subSets.Select(set => GetMyObjectsTask(set.ToArray()));
//breakpoint on the line below gets hit ...
var multiLists = await Task.WhenAll(tasks);
//breakpoint on line below never gets hit ...
var list = new List<MyObject>();
foreach (var myobj in multiLists)
{
list.AddRange(myobj);
}
return list;
}
e sotto è il compito ...
private async Task<IEnumerable<MyObject>> GetMyObjectsTask(params int[] ids)
{
using (var db = new SqlConnection(this.connectionString))
{
//breakpoint on the line below gets hit
await db.OpenAsync();
return await db.QueryAsync<MyObject>(@"SELECT Something FROM Somewhere WHERE ID IN @Ids",
new { ids});
}
}
Il seguente metodo divide solo la lista di id s in blocchi - questo sembra funzionare bene ...
private IEnumerable<IEnumerable<T>> Partition<T>(IEnumerable<T> source, int size)
{
var partition = new List<T>(size);
var counter = 0;
using (var enumerator = source.GetEnumerator())
{
while (enumerator.MoveNext())
{
partition.Add(enumerator.Current);
counter++;
if (counter % size == 0)
{
yield return partition.ToList();
partition.Clear();
counter = 0;
}
}
if (counter != 0)
yield return partition;
}
}
È possibile seguire un debugger tramite GetMyObjectsAsync? Corre fino alla fine? – Stilgar