简体   繁体   中英

Suppress error from Unit of Work transaction

I'm trying to suppress error in code, but MVC action still returns "500 internal server error".

What events are fired in ASP.NET Boilerplate framework after action returns?

public async Task<IActionResult> Post([FromBody]PaymentViewModel model)
{
    var result = false;

    // Storing of card must pass
    try
    {
        // ...
    }
    catch (Exception ex)
    {
        // Catch business exception, but storing
    }

    return Json(new { result });
}

To recover in a catch block, begin a UnitOfWork with RequiresNew :

public async Task<IActionResult> Post([FromBody]PaymentViewModel model)
{
    var result = false;

    // Storing of card must pass
    try
    {
        using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
        {
            // ...

            await CurrentUnitOfWork.SaveChangesAsync();
            await uow.CompleteAsync();
        }
    }
    catch (Exception ex)
    {
        // Catch business exception, but storing
    }

    return Json(new { result });
}

Further explanation: aspnetboilerplate/aspnetboilerplate#2732 (comment)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM