public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { if (!ActionDescriptorHelper.IsControllerActionDescriptor(context.ActionDescriptor)) { await next(); return; } var controllerActionDes = ActionDescriptorHelper.AsControllerActionDescriptor(context.ActionDescriptor); UnitOfWorkOptions options = _miCakeAspNetUowOption.RootUowOptions; if (options == null) { // create default uow options. var currentScope = context.HttpContext.RequestServices as IServiceScope; options = new UnitOfWorkOptions() { Scope = UnitOfWorkScope.Required, ServiceScope = currentScope }; } //has disableTransactionAttribute var actionMehtod = ActionDescriptorHelper.GetActionMethodInfo(context.ActionDescriptor); var hasDisableAttribute = MiCakeUowHelper.IsDisableTransaction(context.Controller.GetType()) || MiCakeUowHelper.IsDisableTransaction(actionMehtod); //has match action key word var hasMatchKeyWord = _miCakeAspNetUowOption.KeyWordToCloseTransaction.Any(keyWord => controllerActionDes.ActionName.ToUpper().StartsWith(keyWord.ToUpper())); options.Scope = hasDisableAttribute || hasMatchKeyWord ? UnitOfWorkScope.Suppress : options.Scope; using var unitOfWork = _unitOfWorkManager.Create(options); var result = await next(); if (Succeed(result)) { await unitOfWork.SaveChangesAsync(); } }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { if (!ActionDescriptorHelper.IsControllerActionDescriptor(context.ActionDescriptor)) { await next(); return; } var controllerActionDes = ActionDescriptorHelper.AsControllerActionDescriptor(context.ActionDescriptor); UnitOfWorkOptions options = _miCakeAspNetUowOption.RootUowOptions ?? new UnitOfWorkOptions(); if (options.Scope != UnitOfWorkScope.Suppress) { //has disableTransactionAttribute var actionMehtod = ActionDescriptorHelper.GetActionMethodInfo(context.ActionDescriptor); var hasDisableAttribute = MiCakeUowHelper.IsDisableTransaction(context.Controller.GetType()) || MiCakeUowHelper.IsDisableTransaction(actionMehtod); //has match action key word var hasMatchKeyWord = _miCakeAspNetUowOption.KeyWordToCloseTransaction.Any(keyWord => controllerActionDes.ActionName.ToUpper().StartsWith(keyWord.ToUpper())); bool needCloseTransaction = hasDisableAttribute || hasMatchKeyWord; options.Scope = needCloseTransaction ? UnitOfWorkScope.Suppress : UnitOfWorkScope.Required; } using (var unitOfWork = _unitOfWorkManager.Create(options)) { var result = await next(); if (Succeed(result)) { await unitOfWork.SaveChangesAsync(); } } }