asp.net mvc项目中修改业务无关的代码为异步执行

荆棘人 / 2023-05-06 / 原文

将业务无关的逻辑,修改为异步执行,示例代码:

public ActionResult SubmitOrder(Order order)
{
    // Save the order to the database synchronously

    // Send email asynchronously using Task
    Task.Run(() => SendEmailAsync(order.RecipientEmail, "Order Confirmation", "Your order has been successfully submitted."));

    // Notify outbound party asynchronously using Task
    Task.Run(() => NotifyOutboundPartyAsync("OutboundEmail@example.com", "A new order has been placed."));

    // Return appropriate response to the client
    return View("OrderConfirmation");
}