Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

346
Views
Usar ConfigureAwait (falso) en tareas pasadas a Task.WhenAll() falla

Estoy tratando de decidir cómo esperar a que se completen todas las tareas asíncronas.

Aquí está el código que tengo actualmente

 [HttpGet] public async Task<JsonResult> doAsyncStuff() { var t1 = this.service1.task1(); var t2 = this.service2.task2(); var t3 = this.service3.task3(); var t4 = this.service4.task4(); await Task.WhenAll(t1,t2,t3,t4); return this.Json(new {redirect = true, href = Url.Action("Blah")}, JsonRequestBehavior.AllowGet); }

Estoy bastante seguro de que el contexto de sincronización no es relevante, así que probé esto.

 [HttpGet] public async Task<JsonResult> doAsyncStuff() { var t1 = this.service1.task1().ConfigureAwait(false); var t2 = this.service2.task2().ConfigureAwait(false); var t3 = this.service3.task3().ConfigureAwait(false); var t4 = this.service4.task4().ConfigureAwait(false); await Task.WhenAll(t1,t2,t3,t4); return this.Json(new {redirect = true, href = Url.Action("Blah")}, JsonRequestBehavior.AllowGet); }

El problema ahora es Task.WhenAll tiene argumentos no válidos porque no aceptará tareas disponibles configuradas.

Entonces Task.WhenAll necesita ser reemplazado con esto

 await t1; await t2; await t3; await t4;

Esto no me parece correcto, sin embargo, prácticamente en todos los lugares donde alguien tiene algo que decir sobre ConfigureAwait es "úselo, si no da error". Y que yo sepa (no escribí las tareas), no usan Synchronous Context, ni confían en él.

Es importante tener en cuenta que me gustaría que la tarea 1 a la tarea 4 se ejecuten al mismo tiempo, ya que no dependen de que finalice la anterior. Como resultado, no quiero esperar frente a cada tarea. Pero no quiero devolver la respuesta Json hasta después de que se completen los 4, por lo que actualmente tengo la espera Task.WhenAll() en el código actual.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Solo necesita ConfigureAwait cuando realmente realiza la await , la forma correcta sería

 [HttpGet] public async Task<JsonResult> doAsyncStuff() { var t1 = this.service1.task1(); var t2 = this.service2.task2(); var t3 = this.service3.task3(); var t4 = this.service4.task4(); await Task.WhenAll(t1,t2,t3,t4).ConfigureAwait(false); return this.Json(new {redirect = true, href = Url.Action("Blah")}, JsonRequestBehavior.AllowGet); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!