public override async Task InterceptAsync(IMethodInvocation invocation) { if (!UnitOfWorkHelper.IsUnitOfWorkMethod(invocation.Method, out var unitOfWorkAttribute)) { await invocation.ProceedAsync(); return; } using (var scope = _serviceScopeFactory.CreateScope()) { var options = CreateOptions(scope.ServiceProvider, invocation, unitOfWorkAttribute); var unitOfWorkManager = scope.ServiceProvider.GetRequiredService <IUnitOfWorkManager>(); //Trying to begin a reserved UOW by UnitOfWorkMiddleware if (unitOfWorkManager.TryBeginReserved(UnitOfWork.UnitOfWorkReservationName, options)) { await invocation.ProceedAsync(); return; } using (var uow = unitOfWorkManager.Begin(options)) { await invocation.ProceedAsync(); await uow.CompleteAsync(); } } }