Forcefield Forcefield - 10 months ago 59
ASP.NET (C#) Question

ASP.NET Web Api SendAsync keeps server waiting

I have seen other very similar questions, but I still haven't found a solution, I have the Postal nuget package installed to handle email, and I have a web method that sends email asynchronously (I suppose). Based on other examples, here is my code:

public async Task<IHttpActionResult> PostEnviarCorreoReserva(
[FromBody] ReservaEmail vermodel,
String ver_gkey)
var ReservaId = Convert.ToInt32(vermodel.Reserva);

CultureInfo es = new CultureInfo("es-ES");
Thread.CurrentThread.CurrentCulture = es;

DtContex = new DTPPublicDataContext();

var RSPD = DtContex.res_reservas_usuario_det.First(i => i.reserva_gkey == ReservaId);
dynamic emailReserva = new Email(TipoEmail);
emailReserva.To = RSPD.email_reserva;
emailReserva.CodReserva = RSPD.reserva_gkey.ToString();


await emailReserva.SendAsync();

return Ok();

So I'm still a newbie, but I understand that this code should execute asynchronously, so I can later perform other operations to the Web API, but until it return the Ok response, Web API is busy handling this threat, what exactly Im doing wrong? Sending email takes a really long time

Answer Source

If you don't want to wait, while sendout will be completed, execute code in new thread.

new Thread(async () =>
    await emailReserva.SendAsync();